Android 學習之逐幀動畫(Frame)
阿新 • • 發佈:2017-05-11
http sta ram override start pub fill creat 代碼
當中oneshot代表的是否循環播放,false是循環播放,true是僅僅播放一次
c:獲得AnimationDrawable對象
d:開始播放動畫就ok
Activity整個代碼:
演示效果:
幀動畫就是將一些列圖片。依次播放。
利用肉眼的“視覺暫留”的原理,給用戶的感覺是動畫的錯覺,逐幀動畫的原理和早期的電影原理是一樣的。
a:須要定義逐幀動畫,能夠通過代碼定義。也能夠通過XML文件定義。一般XML文件定義比較直觀
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <!--false是循環播放 --> <!-- drawables是每一張圖片。 duration是圖片持續的時間 --> <item android:drawable="@drawable/g1" android:duration="200" /> <item android:drawable="@drawable/g2" android:duration="200" /> <item android:drawable="@drawable/g3" android:duration="200" /> <item android:drawable="@drawable/g4" android:duration="200" /> <item android:drawable="@drawable/g5" android:duration="200" /> <item android:drawable="@drawable/g6" android:duration="200" /> <item android:drawable="@drawable/g7" android:duration="200" /> <item android:drawable="@drawable/g8" android:duration="200" /> <item android:drawable="@drawable/g9" android:duration="200" /> <item android:drawable="@drawable/g10" android:duration="200" /> <item android:drawable="@drawable/g11" android:duration="200" /> </animation-list>
當中oneshot代表的是否循環播放,false是循環播放,true是僅僅播放一次
b:將上述的XMLd定義的資源,設置為ImageView的背景
//找到imageview ImageView iv = (ImageView) findViewById(R.id.iv); //將幀動畫的資源文件設置為imageview的背景 iv.setBackgroundResource(R.drawable.frameanimation);
c:獲得AnimationDrawable對象
//獲取AnimationDrawable對象 AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
d:開始播放動畫就ok
//開始播放動畫 ad.start();
Activity整個代碼:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到imageview ImageView iv = (ImageView) findViewById(R.id.iv); //將幀動畫的資源文件設置為imageview的背景 iv.setBackgroundResource(R.drawable.frameanimation); //獲取AnimationDrawable對象 AnimationDrawable ad = (AnimationDrawable) iv.getBackground(); //開始播放動畫 ad.start(); } }
演示效果:
Android 學習之逐幀動畫(Frame)