一、 alpha:漸變透明度動畫效果
1. 代碼方式實現(xiàn)
AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f);
alphaAnimation.setDuration(1000);
imageView.startAnimation(alphaAnimation);
- 第一個參數(shù):起始透明度摩渺。
- 第二個參數(shù):結束透明度。
- 0.0f 全透明
1.0f 不透明
2. xml 方式實現(xiàn)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
Animation alphaAnimation=AnimationUtils.loadAnimation(this, R.anim.alpha); //加載Xml文件中的動畫
imageView.startAnimation(alphaAnimation);
二、 scale:漸變尺寸伸縮動畫效果
1. 代碼方式實現(xiàn)
ScaleAnimation scaleAnimation=new ScaleAnimation(0.0f,1.0f,0.0f,1.0f,
ScaleAnimation.RELATIVE_TO_SELF,1.0f,ScaleAnimation.RELATIVE_TO_SELF,0.0f);
scaleAnimation.setDuration(1000);
imageView.startAnimation(scaleAnimation);
- 第一個參數(shù):起始時 X坐標上的伸縮尺寸为肮。
- 第二個參數(shù):結束時 X坐標上的伸縮尺寸。
- 第三個參數(shù):起始時 Y坐標上的伸縮尺寸肤京。
- 第四個參數(shù):結束時 Y坐標上的伸縮尺寸颊艳。
- 0.0f表示收縮到?jīng)]有
1.0f表示正常無伸縮
值小于1.0f表示收縮
值大于1.0f表示放大 - 第五個參數(shù):動畫在X軸相對于物件位置類型。
- 第六個參數(shù):動畫相對于物件的X坐標的開始位置忘分。
- 第七個參數(shù):動畫在Y軸相對于物件位置類型棋枕。
- 第八個參數(shù):動畫相對于物件的Y坐標的開始位置。
- 0.0f妒峦,0.0f 在左上角
0.0f重斑,1.0f 在左下角
1.0f,1.0f 在右下角
0.5f肯骇,0.5f 在中間
2. xml 方式實現(xiàn)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="1000"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:toXScale="1.4"
android:toYScale="1.4"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%" />
</set>
Animation scaleAnimation=AnimationUtils.loadAnimation(this, R.anim.scale); //加載Xml文件中的動畫
imageView.startAnimation(scaleAnimation);
三窥浪、translate:畫面轉換位置移動動畫效果
1. 代碼方式實現(xiàn)
TranslateAnimation translateAnimation=new TranslateAnimation(0,100,0,100);
translateAnimation.setDuration(1000);
translateAnimation.setInterpolator(this, android.R.anim.cycle_interpolator);
translateAnimation.setFillAfter(true);
imageView.startAnimation(translateAnimation);
- 第一個參數(shù):動畫起始時 X坐標上的移動位置。
- 第二個參數(shù):**動畫結束時 X坐標上的移動位置 **笛丙。
- 第三個參數(shù):**動畫起始時 Y坐標上的移動位置 **漾脂。
- 第四個參數(shù):動畫結束時 Y坐標上的移動位置。
2. xml 方式實現(xiàn)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="2000"
android:fromXDelta="30"
android:fromYDelta="30"
android:toXDelta="-80"
android:toYDelta="300" />
</set>
Animation translateAnimation=AnimationUtils.loadAnimation(this, R.anim.translate); //加載Xml文件中的動畫
imageView.startAnimation(translateAnimation);
四胚鸯、rotate:畫面轉移旋轉動畫效果
1. 代碼方式實現(xiàn)
RotateAnimation rotateAnimation=new RotateAnimation(0.0f,360.0f,
Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
rotateAnimation.setFillAfter(true);
imageView.startAnimation(rotateAnimation);
- 第一個參數(shù):動畫起始時的旋轉角度
- 第二個參數(shù):動畫旋轉到的角度
- 第三個參數(shù):動畫在X軸相對于物件位置類型
- 第四個參數(shù):動畫相對于物件的X坐標的開始位置
- 第五個參數(shù):動畫在Y軸相對于物件位置類型
- 第六個參數(shù):動畫相對于物件的Y坐標的開始位置
2. xml 方式實現(xiàn)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="3000"
android:fromDegrees="0"
android:toDegrees="350"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%" />
</set>
Animation rotateAnimation=AnimationUtils.loadAnimation(this, R.anim.rotate); //加載Xml文件中的動畫
imageView.startAnimation(rotateAnimation);
五骨稿、通用方法
-
setDuration(long durationMills)
設置動畫持續(xù)時間(單位:毫秒) -
setFillAfter(Boolean fillAfter)
如果fillAfter的值為true,則動畫執(zhí)行后,控件將停留在執(zhí)行結束的狀態(tài) -
setFillBefore(Boolean fillBefore)
如果fillBefore的值為true姜钳,則動畫執(zhí)行后坦冠,控件將回到動畫執(zhí)行之前的狀態(tài) -
setStartOffSet(long startOffSet)
設置動畫執(zhí)行之前的等待時間 -
setRepeatCount(int repeatCount)
設置動畫重復執(zhí)行的次數(shù)
六、Interpolator:動畫插入器
animation.setInterpolator(this, android.R.anim.cycle_interpolator);
animation.setInterpolator(new AccelerateInterpolator());
或
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
-
AccelerateInterpolator
加速哥桥,開始時慢中間加速 -
DecelerateInterpolator
減速辙浑,開始時快然后減速 -
AccelerateDecelerateInterolator
先加速后減速,開始結束時慢拟糕,中間加速 -
AnticipateInterpolator
反向判呕,先向相反方向改變一段再加速播放 -
AnticipateOvershootInterpolator
反向加超越,先向相反方向改變已卸,再加速播放佛玄,會超出目的值然后緩慢移動至目的值 -
BounceInterpolator
彈球效果,快到目的值時值會跳躍累澡,如目的值100梦抢,后面的值可能依次為85,77愧哟,70奥吩,80哼蛆,90,100 -
CycleIinterpolator
循環(huán)霞赫,動畫循環(huán)一定次數(shù)腮介,值的改變?yōu)橐徽液瘮?shù):Math.sin(2 x mCycles x Math.PI x input) -
LinearInterpolator
線性,線性均勻改變 -
OvershottInterpolator
超越端衰,最后超出目的值然后緩慢改變到目的值
七叠洗、AnimationSet 實現(xiàn)多種動畫混合效果
- 可同時播放,也可以給不同動畫分別設置 setStartOffset() 開始偏移來順序播放旅东。
1. 代碼方式實現(xiàn)
AnimationSet animationSet=new AnimationSet(true);
AlphaAnimation alphaAnimation=AnimationUtils.loadAnimation(this, R.anim.alpha);
ScaleAnimation scaleAnimation=AnimationUtils.loadAnimation(this, R.anim.scale);
TranslateAnimation translateAnimation=new TranslateAnimation(0,100,0,100);
RotateAnimation rotateAnimation=new RotateAnimation(0.0f,360.0f,
Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF, 0.5f);
alphaAnimation.setStartOffset(0);
scaleAnimation.setStartOffset(2000);
translateAnimation.setStartOffset(4000);
rotateAnimation.setStartOffset(6000);
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.setInterpolator(this, android.R.anim.anticipate_interpolator);
animationSet.setDuration(2000);
imageView.startAnimation(animationSet);
2. xml 方式實現(xiàn)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500"/>
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="2000"/>
......
</set>
AnimationSet animationSet=(AnimationSet) AnimationUtils.loadAnimation(this, R.anim.animset);