一、使用xml文件
res\anim中新建xml文件
1、平移
<!--float fromXDelta 动画开始的点离当前View X坐标上的差值 -->
<!--float toXDelta 动画结束的点离当前View X坐标上的差值 -->
<!--float fromYDelta 动画开始的点离当前View Y坐标上的差值 -->
<!--float toYDelta 动画开始的点离当前View Y坐标上的差值 -->
<!--android:duration:运行动画的时间android:repeatCount:定义动画重复的次数 -->
<!--animation.setDuration(long durationMillis);//设置动画持续时间 -->
<!-- animation.setRepeatCount(int i);//设置重复次数 -->
<!-- animation.setRepeatMode(Animation.REVERSE);//设置反方向执行 -->
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromYDelta="0.0"
android:toYDelta="100.0"
android:fromXDelta="0.0"
android:toXDelta="100"
android:repeatCount="2"/>
2、旋转<!-- fromDegrees动画开始时的角度
toDegrees 动画结束时物件的旋转角度,正代表顺时针
pivotX 属性为动画相对于控件的X坐标的开始位置
pivotY 属性为动画相对于控件的Y坐标的开始位置
50% 表示中间-->
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromDegrees="-360"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"/>
3、缩放
<!-- fromXDelta,fromYDelta 起始时 X , Y 座标 , 屏幕右下角的座标是 X:320,Y:480
toXDelta, toYDelta动画结束时X,Y的座标
interpolator 指定动画插入器
常见的有加速减速插入器 accelerate_decelerate_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,动画重复的计数
repeatMode,动画重复的模式, reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变-->
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/decelerate_interpolator"
android:fromXScale="0.0"
android:toXScale="1.5"
android:fromYScale="0.0"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
android:duration="10000"
android:repeatCount="1"
android:repeatMode="reverse" />
4、透明度
<!-- fromAlpha:开始时透明度
toAlpha: 结束时透明度-->
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1"/>
5、合起来
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
/>
<scale
/>
<translate
/>
<alpha
/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
/>
<scale
/>
<translate
/>
<alpha
/>
</set>
调用代
mBt_tran = (Button) findViewById(R.id.bt_am_tran); mBt_tran.startAnimation(AnimationUtils.loadAnimation(this, R.anim.anim_tran));
二、代码写
//旋转动画 RotateAnimation rotateAnimation = new RotateAnimation(-360, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(1000); rotateAnimation.setFillAfter(true); //缩放动画 ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(1000); scaleAnimation.setFillAfter(true); //渐变动画 AlphaAnimation alphaAnimation = new AlphaAnimation(0.2f, 0.8f); alphaAnimation.setDuration(1000); alphaAnimation.setFillAfter(true); //平移动画 TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 100, 100); translateAnimation.setDuration(1000); translateAnimation.setFillAfter(true); //动画集合 AnimationSet animationSet = new AnimationSet(true); animationSet.addAnimation(rotateAnimation); animationSet.addAnimation(scaleAnimation); animationSet.addAnimation(alphaAnimation); animationSet.addAnimation(translateAnimation);
调用代码
//启动动画 mBt_tran = (Button) findViewById(R.id.bt_am_tran); mBt_tran.startAnimation(animationSet);
setFillAfter(boolean fillAfter)
如果fillafter转变,true,动画将坚持执行完成时。(管不管用不好说)
实际只能改变动画的属性,不能改变view属性
假定你有一个移动的动画紧跟一个淡出的动画,如果你不把移动的动画的setFillAfter置为true,那么移动动画结束后,View会回到原来的位置淡出,如果setFillAfter置为true, 就会在移动动画结束的位置淡出
,