- 透明度(AlphaAnimation)
- 旋轉(RotateAnimation)
- 縮放(ScaleAnimation)
- 位移(TranslateAnimation)
- 動畫集合(AnimationSet)
爲視圖增加透明度變換的動畫
AlphaAnimation aa = new AlphaAnimation(0, 1);//透明度變化範圍
aa.setDuration(1000);
view.startAnimation(aa);
RotateAnimation ra = new RotateAnimation(0, 360, //起始角度及最終角度
100, 100);//旋轉中心點的座標
ra.setDuration(1000);
view.startAnimation(ra);
RotateAnimation ra = new RotateAnimation(0,360,
RotateAnimation.RELATIVE_TO_SELF,//設置參考系爲自身中心點,也可設置爲父容器爲參考系
RotateAnimation.RELATIVE_TO_SELF);
ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2);//縮放的起始座標及最終座標
ScaleAnimation sa = new ScaleAnimation(0,1,0,1,
ScaleAnimation.RELATIVE_TO_SELF,//設置以自身中心爲縮放中心
ScaleAnimation.RELATIVE_TO_SELF);
sa.setDuration(1000);
view.startAnimation(sa);
TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 300);//位移的起始座標及最終座標
ta.setDuration(1000);
view.startAnimation(ta);
AnimationSet as = new AnimationSet(true);
as.setDuration(1000);
AlphaAnimation aa = new AlphaAnimation(0, 1);
aa.setDuration(1000);
as.addAnimation(aa);
RotateAnimation ra = new RotateAnimation(0, 360, 100, 100);
ra.setDuration(1000);
as.addAnimation(ra);
TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 300);
ta.setDuration(1000);
as.addAnimation(ta);
ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2);
sa.setDuration(1000);
as.addAnimation(sa);
view.startAnimation(as);
as.setAnimationListener(new Animation.AnimationListener() {//監聽動畫事件
@Override
public void onAnimationStart(Animation animation) {//開始
}
@Override
public void onAnimationEnd(Animation animation) {//結束
}
@Override
public void onAnimationRepeat(Animation animation) {//重複
}
});
這四種動畫實現方式都是通過Animation類和AnimationUtils配合實現。
也可以通過xml實現:動畫的XML文件在工程中res/anim目錄。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@[package:] anim/interpolator_resource"
android:shareInterpolator="true">
<!--透明度動畫
fromAlpha 透明度起始值,比如0.1
toAlpha 透明度結束值,比如1
-->
<alpha
android:fromAlpha="float"
android:toAlpha="float"/>
<!--縮放動畫
fromXScale 水平縮放的起始值,比如0.5
toXScale 水平縮放的結束值,比如1.2
pivotX/pivotY 中縮放心座標點的x,y座標
-->
<scale
android:fromXScale="float"
android:fromYScale="float"
android:toXScale="float"
android:toYScale="float"
android:pivotX="float"
android:pivotY="float"/>
<!--平移動畫
fromXDelta x的起始值,比如0
toXDelta x的結束值,比如100
-->
<translate
android:fromXDelta="float"
android:fromYDelta="float"
android:toXDelta="float"
android:toYDelta="float"/>
<!--旋轉動畫
fromDegrees 旋轉開始的角度,比如0
toDegrees 旋轉結束的角度,比如180
pivotX/pivotY 中心座標點
-->
<rotate
android:fromDegrees="float"
android:pivotX="float"
android:pivotY="float"
android:toDegrees="float"/>
<set>
....
</set>
</set>
- android:duration 動畫的持續時間
- android:fillAfter 動畫結束後View 是否停留在結束位置,true表示 View 停留在結束位置,false則不停留
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter = "false"
android:zAdjustment="bottom"
>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="4000"
/>
</set>
如何應用上面的動畫呢?如下所示:
Button mButton = (Button)findViewById(R。id.button);
Animation animation = AnimationUtils.loadAnimation(this,R.anim.animation_test);
mButton.startAnimation(animation);
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
>
<item android:drawable="@drawable/p1" android:duration="1000"></item>
<item android:drawable="@drawable/p2" android:duration="1000"></item>
<item android:drawable="@drawable/p3" android:duration="1000"></item>
<item android:drawable="@drawable/p4" android:duration="1000"></item>
<item android:drawable="@drawable/p5" android:duration="1000"></item>
<item android:drawable="@drawable/p6" android:duration="1000"></item>
</animation-list>
使用動畫
AnimationDrawable anim = (AnimationDrawable)getResources().
getDrawable(R.drawable.frame);
textWidget = (TextView)findViewById(R.id.text_widget);
textWidget.setText("背景漸變動畫效果");
textWidget.setBackgroundDrawable(anim);
anim.start();
這裏有點不同的是,利用AnimationDrawable實現動畫時,本身並沒有提供接口來監聽動畫的狀態(開始,結束),需要自己處理。幀動畫使用比較簡單,但是比較容易引起OOM,所以在使用幀動畫時應儘量避免使用過多尺寸較大的圖片