Android各种动画效果ScaleAnimation,AlphaAnimation,TranslateAnimation,RotateAnimation(文章结尾有代码)
来源:互联网 发布:什么软件马赛克好看 编辑:程序博客网 时间:2024/05/17 22:14
在各种应用中,通常会使用到多种动画效果,在Android中常见的动画有四种:Scale,Alpha,Translate,Rotate。Android系统也很人性化的给出了利用xml方式和利用Java代码方式进行设置。以下分别进行介绍,算是给自己做个总结。
首先给出工程的地址:Android各种动画效果Scale,Alpha,Translate,Rotate
1.ScaleAnimation动画
xml方式进行设置:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="2000" android:fromXScale="0.0" android:fromYScale="0.0" android:interpolator="@android:anim/decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="2" android:repeatMode="reverse" android:startOffset="100" android:toXScale="1.2" android:toYScale="1.2" /> <!--fromXDelta,fromYDelta 动画起始时X,Y座标 toXDelta, toYDelta 动画结束时X,Y的座标 --> <!--interpolator 动画所使用的类型:常见的有加速减速插入器:加速插入器 accelerate_interpolator;速插入器 decelerate_interpolator。fromXScale,fromYScale, 动画开始前X,Y的缩放,0.0为不显示, 1.0为正常大小 toXScale,toYScale, 动画最终缩放的倍数, 1.0为正常大小,大于1.0放大 pivotX, pivotY 动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从屏幕中间开始 startOffset, 动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒 duration,一次动画效果消耗的时间,单位毫秒, repeatCount, 动画重复的计数,动画将会执行该值+1次 repeatMode, 动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。 restart重新执行,方向不变 --></set>工程中引用:
anim = AnimationUtils.loadAnimation(mContext, R.anim.scale_test);//设置动画开始,结束时候的监听anim.setAnimationListener(this);img.startAnimation(anim);Java代码方式设置:
anim = new ScaleAnimation(0.0f, 1.2f, 0.0f, 1.2f,ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);//Animation.ABSOLUTE:绝对坐标 //Animation.RELATIVE_TO_PARENT:相对父控件 //Animation.RELATIVE_TO_SELF:相对自己 anim.setDuration(2000);//所有属性可以通过set函数设置anim.setAnimationListener(this);//进行下一个动画时候要清除上一个动画img.startAnimation(anim);2.AlphaAnimation动画
xml方式:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2300" android:repeatCount="2" android:repeatMode="reverse" android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="100" android:background="@android:color/holo_green_light" /> <!-- fromAlpha:开始时透明度 toAlpha: 结束时透明度 duration:动画持续时间 --></set>工程中引用:
anim = AnimationUtils.loadAnimation(mContext, R.anim.alpha_test);anim.setAnimationListener(this);img.startAnimation(anim);Java代码设置:
anim = new AlphaAnimation(1.0f, 0.0f);anim.setDuration(2000);img.startAnimation(anim);3.TranslateAnimation动画
xml方式:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="2000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="690" android:repeatCount="1" android:repeatMode="reverse" android:toYDelta="0" /> <!--fromXDelta,fromYDelta 动画起始时X,Y的座标 toXDelta,toYDelta 动画结束时X,Y的座标 --></set>在工程中的引用代码:
anim = AnimationUtils.loadAnimation(mContext, R.anim.tranlate_test);anim.setAnimationListener(this);img.startAnimation(anim);
Java代码方式
anim = new TranslateAnimation(0.0f, 690f, 0f, 0f);anim.setAnimationListener(this);img.startAnimation(anim);4.RotateAnimation动画
xml方式:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <rotate android:duration="5000" android:fromDegrees="300" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toDegrees="-360" /> <!-- fromDegrees 动画开始时的角度 toDegrees 动画结束时物件的旋转角度,正代表顺时针 pivotX 属性为动画相对于物件的X坐标的开始位置 pivotY 属性为动画相对于物件的Y坐标的开始位置 --></set>在工程中引用:
anim = AnimationUtils.loadAnimation(mContext, R.anim.rotate_test);anim.setAnimationListener(this);img.startAnimation(anim);Java代码设置:
anim = new RotateAnimation(0.0f, -360f, 540/2f, 540/2f);anim.setAnimationListener(this);anim.setDuration(2000);anim.setRepeatCount(2);anim.setRepeatMode(RotateAnimation.RESTART);img.startAnimation(anim);5.多个动画合并
xml方式:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <rotate android:duration="5000" android:fromDegrees="300" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toDegrees="-360" /> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2300" android:repeatCount="2" android:repeatMode="reverse" android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="100" android:background="@android:color/holo_green_light" /> </set>在工程中的引用:
Animation a1 = AnimationUtils.loadAnimation(mContext, R.anim.alpha_test);Animation a2 = AnimationUtils.loadAnimation(mContext, R.anim.rotate_test);AnimationSet set = new AnimationSet(mContext,null);set.addAnimation(a1);set.addAnimation(a2);set.setAnimationListener(this);img.startAnimation(set);其中在上面用到的监听是:
new AnimationListener() {/** * 动画开始前的回调 */@Overridepublic void onAnimationStart(Animation animation) {if (flag) {//看当前img是否在动画,是就取消img.clearAnimation();flag = false;}else{flag = true;}Log.d(TAG, "start");}/** * 动画重复时的回调 */@Overridepublic void onAnimationRepeat(Animation animation) {Log.d(TAG, "repeat");}/** * 动画结束后的回调 */@Overridepublic void onAnimationEnd(Animation animation) {Log.d(TAG, "end");flag = false;}}从上面代码可以看出,几个动画效果设置都差不多,会一个其他的都是相通的。
工程的地址:Android各种动画效果Scale,Alpha,Translate,Rotate
1 0
- Android各种动画效果ScaleAnimation,AlphaAnimation,TranslateAnimation,RotateAnimation(文章结尾有代码)
- Android中的动画(AlphaAnimation、RotateAnimation、ScaleAnimation、TranslateAnimation)
- android 四种动画TranslateAnimation,ScaleAnimation,RotateAnimation,AlphaAnimation
- 视图动画:RotateAnimation、TranslateAnimation、ScaleAnimation、AlphaAnimation
- 补间动画TranslateAnimation、ScaleAnimation、AlphaAnimation、RotateAnimation、AnimationSet
- Android补间动画之ScaleAnimation、AlphaAnimation、RotateAnimation、TranslateAnimation、AnimationSet详解
- AnimationSet,AlphaAnimation,RotateAnimation,ScaleAnimation
- ScaleAnimation、RotateAnimation、ScaleAnimation、TranslateAnimation详解
- 【Android动画九章】-RotateAnimation(旋转动画)和ScaleAnimation(尺寸动画)
- rotateAnimation 动画效果(转载)
- Android AlphaAnimation\TranslateAnimation 多组动画循环+单个动画结束后闪烁问题
- AlphaAnimation动画效果
- ScaleAnimation 缩放动画效果
- Android 动画之AlphaAnimation应用详解(透明度动画效果)转载
- Android 动画之ScaleAnimation应用详解(缩放动画效果 )(转载)
- TranslateAnimation 位移动画效果
- Android动画之AlphaAnimation
- AndroidUI 视图动画-透明动画效果 (AlphaAnimation)
- 跟着鬼哥学Android java hook(一)
- 解决模态窗口按回车键出现很多窗口bug
- python 除法
- 【学习】对于AndroidStudio 中 Local History还原文件的理解
- (Plugin with id com.android.application not found)问题解决
- Android各种动画效果ScaleAnimation,AlphaAnimation,TranslateAnimation,RotateAnimation(文章结尾有代码)
- Eclipse去除js(JavaScript)验证错误
- OpenResty学习笔记(十一) nginx做负载均衡
- GPIOLIB实现(一)
- __declspec(dllexport)、__declspec(dllimport)详解
- 移动页面HTML5自适应手机屏幕宽度
- 基于HTML5+WebSocket+JAVA的棋牌游戏开发,从入门到放弃(二)
- 百练 02 滑雪
- 大话设计模式06----装饰模式