在代碼中可以通過set來設置多個動畫屬性,這里分開來設置不同的屬性。
首先先貼上布局文件,里面的imageview是用來做動畫的控件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="28dp" android:orientation="vertical" > <Button android:id="@+id/alpha_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="buttonListener" android:text="透明度改變" /> <Button android:id="@+id/rotate_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="buttonListener" android:text="旋轉動畫" /> <Button android:id="@+id/scale_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="buttonListener" android:text="縮放動畫" /> <Button android:id="@+id/translate_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="buttonListener" android:text="移動動畫" /> </LinearLayout> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/linearLayout1" android:layout_centerHorizontal="true" android:layout_marginTop="56dp" android:src="@drawable/ic_launcher" /> </RelativeLayout>
在activity中的listener中寫上不同的動畫效果
主要是
AnimationSet set = new AnimationSet(true);
set.addAnimation(alpha);
package com.kale.anim; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; public class MainActivity extends Activity { ImageView iV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iV = (ImageView)findViewById(R.id.imageView); } public void buttonListener(View v) { AnimationSet set = new AnimationSet(true); switch (v.getId()) { case R.id.alpha_button: //設置漸變從不透明->透明,1表示不透明,0表示透明 AlphaAnimation alpha = new AlphaAnimation(1f, 0f); //設置執行的時間 alpha.setDuration(1000); set.addAnimation(alpha); break; case R.id.rotate_button: //RotateAnimation rotate = new RotateAnimation(0, 180, 0, 0);//從0度旋轉到180度,以左上角(0,0)為圓心 //從相對於自身(圓心在圖片的中心)旋轉360度, //x軸坐標相對於父控件寬的一半,y軸相對於自身高的一半,於是確定一個圓心 RotateAnimation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_PARENT, 0.5f, //0.5 = 1/2的自己父控件的長度 Animation.RELATIVE_TO_SELF, 0.5f);//0.5 = 1/2的自己的長度 rotate.setDuration(5000); set.addAnimation(rotate); break; case R.id.scale_button: //縮放動畫,x坐標從1f->2f,y坐標從1f->2f。縮放的軸是相對於自己的一半,等於是自己的中心 ScaleAnimation scale = new ScaleAnimation(1f, 2f, 1f, 2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scale.setDuration(1000); set.addAnimation(scale); break; case R.id.translate_button: //移動動畫.x:從相對於自己x軸為0的位置移動到相對於自己x軸為1的位置。等於自己向右邊移動一個身位 //y:從相對於自己y軸為0的位置移動到相對於自己y軸為1的位置。等於自己向下移動了兩個身位 TranslateAnimation translate = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 2f); translate.setDuration(1000); set.addAnimation(translate); break; default: break; } //設置開始動畫 iV.startAnimation(set); } }
補充:
set.setStartOffset(1000);//一秒后再執行動畫 = 等待1秒后執行動畫 set.setFillAfter(true);//設置動畫執行后保持最后狀態 set.setFillBefore(false);//設置動畫執行后不回到原來狀態 set.setRepeatCount(3);//設置動畫重復執行的次數