一.Android動畫類型
Android的animation由四種類型組成:
XML中
alph | 漸變透明度動畫效果 |
scale | 漸變尺寸伸縮動畫效果 |
translate | 畫面轉換位置移動動畫效果 |
rotate | 畫面轉移旋轉動畫效果 |
JavaCode中
AlphaAnimation | 漸變透明度動畫效果 |
ScaleAnimation | 漸變尺寸伸縮動畫效果 |
TranslateAnimation | 畫面轉換位置移動動畫效果 |
RotateAnimation | 畫面轉移旋轉動畫效果 |
二.Android動畫模式
Animation主要有兩種動畫模式:
一種是tweened animation(漸變動畫)
XML中 | JavaCode |
alpha | AlphaAnimation |
scale | ScaleAnimation |
一種是frame by frame(畫面轉換動畫)
XML中 | JavaCode |
translate | TranslateAnimation |
rotate | RotateAnimation |
三.Android動畫實現
1.通過代碼實現動畫效果
(1)透明度漸變效果
//1.創建透明度動畫對象,數值越小越透明 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0.1f); //設置動畫的持續時間 alphaAnimation.setDuration(3000); //設置是否保留最終狀態 // alphaAnimation.setFillAfter(true); //設置重復次數,填-1無限循環 alphaAnimation.setRepeatCount(2); //設置動畫的重復模式,默認是Restart,Reverse是反方向執行 alphaAnimation.setRepeatMode(Animation.REVERSE); //通過控件啟動動畫 imageView.startAnimation(alphaAnimation);
(2)縮放效果
//相對控件自己的左上角為原點縮放 // ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f, 2, 0.2f, 2); //相對點(200,300)進行縮放 // ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f,2,0.2f,2,200,300); //相對控件自己的中心點進行縮放 // ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f,2,0.2f,2,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); //相對父容器的中心點進行縮放 // fromX:開始縮放的X軸倍數。如1.0f:本身大小;如2.0f:從自己兩倍開始 // toX:結束縮放的X軸倍數。同上... // fromY:始縮放的Y軸倍數。 // toY:結束縮放的Y軸倍數。 /** pivotXType:X軸縮放中心點類型;可選值有: Animation.RELATIVE_TO_SELF相對自己--常用 Animation.RELATIVE_TO_PARENT相對父窗體 Animation.ABSOLUTE 絕對的---不常用 */ // pivotXValue:在pivotXType的基礎上,X軸縮放中心的位置。如:0.5f:縮放中心就在控件的一半的位置。如果是0.0f,則會在控件本身的左邊位置 // pivotYType:X軸縮放中心點類型;同上 ... // pivotYValue:在pivotYType的基礎上,Y軸縮放中心的位置。 ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 2, 0.5f, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(3000); imageView.startAnimation(scaleAnimation);
(3)旋轉效果
//相對自己的左上角旋轉,正數代表順時針,負數逆時針 RotateAnimation rotateAnimation = new RotateAnimation(0,-180); //相對(200,300)點旋轉 //RotateAnimation rotateAnimation = new RotateAnimation(0,-180,200,300); rotateAnimation.setDuration(3000); imageView.startAnimation(rotateAnimation);
(4)平移效果
/*new TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue); 例如:從控件自己的位置開始,向下移動自己的一倍距離。*/ //TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, // Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f); // TranslateAnimation translateAnimation = new TranslateAnimation(0,200,0,0); //獲得屏幕寬度: int screenWidth = getResources().getDisplayMetrics().widthPixels; int imgeWidth = imageView.getWidth(); //TranslateAnimation translateAnimation = new TranslateAnimation(0,screenWidth-imgeWidth,0,0); TranslateAnimation translateAnimation = new TranslateAnimation(0,200,0,0); translateAnimation.setDuration(3000); translateAnimation.setFillAfter(true); imageView.startAnimation(translateAnimation);
(5)動畫集合效果
@Override public void onCreate(Bundle savedInstanceState) { //重載onCreate方法 super.onCreate(savedInstanceState); setContentView(R.layout.main); final ImageView image=(ImageView)findViewById(R.id.imageView1); //ImageView對象 Button btn1=(Button)findViewById(R.id.button1); //按鈕對象 Button btn2=(Button)findViewById(R.id.button2); final Animation translateAnimation=new TranslateAnimation(0,300,0,300); //設置位置變化動畫 final Animation scaleAnimation = new ScaleAnimation(0f,1f,0f,1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); //設置尺寸變化動畫 final Animation alphaAnimation=new AlphaAnimation(0.1f,1.0f); //設置透明度變化動畫 btn1.setOnClickListener(new View.OnClickListener() { //設置監聽器 @Override public void onClick(View v) { // TODO Auto-generated method stub translateAnimation.setDuration(10000); //設置位置變化動畫的持續時間 scaleAnimation.setDuration(10000); //設置尺寸變化動畫的持續時間 alphaAnimation.setDuration(10000); //設置透明度漸變動畫的持續時間 AnimationSet set=new AnimationSet(true); //創建動畫集對象 set.addAnimation(translateAnimation); //添加位置變化動畫 set.addAnimation(scaleAnimation); //添加尺寸變化動畫 set.addAnimation(alphaAnimation); //添加透明度漸變動畫 set.setFillAfter(true); //停留在最后的位置 set.setFillEnabled(true); image.setAnimation(set); //設置動畫 set.startNow(); //啟動動畫 } }); btn2.setOnClickListener(new View.OnClickListener() { //設置監聽器 @Override public void onClick(View v) { // TODO Auto-generated method stub set.cancel(); //取消動畫執行 } }); }
2.通過xml實現動畫效果
(1)透明度漸變效果
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:repeatMode="reverse"> <alpha android:fromAlpha="0.2" android:toAlpha="1" android:repeatCount="infinite"/> </set>
(2)縮放效果
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000"> <scale android:fromXScale="0.2" android:toXScale="1" android:pivotX="50%" android:pivotY="50%" android:fromYScale="0.2" android:toYScale="1"/> </set>
(3)旋轉效果
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:repeatMode="reverse"> <alpha android:fromAlpha="0.2" android:toAlpha="1" android:repeatCount="infinite"/> <rotate android:fromDegrees="0" android:toDegrees="180" android:pivotX="50%p" android:pivotY="50%p"/> <!--android:pivotX="50%p" 50%p代表的是相對父容器的中心點旋轉 android:pivotX="50%" 50%代表的是相對自己的中心點旋轉 --> </set>
(4)平移效果
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fillAfter="true" android:interpolator="@android:anim/overshoot_interpolator"> <!-- android:fillAfter="true" 設置是否保留最終狀態 如果是集合,需要在set節點中設置 android:interpolator="@android:anim/overshoot_interpolator" 如果有集合,插值器要放在集合的屬性中--> <translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="400" android:toYDelta="0" > <!-- android:repeatCount="infinite" 無限重復 只能在子節點中單獨設置--> </translate> </set>
(5)加載xml動畫效果
//加載動畫資源 Animation animation = AnimationUtils.loadAnimation(this, R.anim.translation_anim); //開啟動畫 imageView.startAnimation(animation);