Android 逐幀動畫創(chuàng)建實例詳解
Android 逐幀動畫創(chuàng)建實例詳解
前言:
我們看早期電影的時候,電影通常是一張一張播放,用我們現(xiàn)在專有名詞來說,就是一幀幀來,安卓同樣有這樣動畫效果的編排形式。
那么我們先定義逐幀動畫xml文件
<?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/pic1"
android:duration="200" />
<item
android:drawable="@drawable/pic2"
android:duration="200" />
<item
android:drawable="@drawable/pic3"
android:duration="200" />
<item
android:drawable="@drawable/pic4"
android:duration="200" />
<item
android:drawable="@drawable/pic5"
android:duration="200" />
<item
android:drawable="@drawable/pic6"
android:duration="200" />
<item
android:drawable="@drawable/pic7"
android:duration="200" />
<item
android:drawable="@drawable/pic8"
android:duration="200" />
<item
android:drawable="@drawable/pic8"
android:duration="200" />
</animation-list>
main.xml
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="98dp"
android:layout_marginTop="69dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="54dp"
android:layout_marginLeft="98dp"
android:onClick="startMovie"
android:text="開始播放電影" />
Activiy代碼:
public class MyAnimationDemo extends Activity {
private AnimationDrawable draw=null;
private ImageView image;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_animation_demo);
image=(ImageView)super.findViewById(R.id.pic);
}
public void startMovie(View v){
image.setBackgroundResource(R.anim.oldvideo);//第一步,設(shè)置圖片資源
draw=(AnimationDrawable)image.getBackground();//取得圖片背景的Drawable
draw.setOneShot(false);//動畫執(zhí)行次數(shù)
draw.start();//開始動畫
}
}
這里我們看到,
第一步,設(shè)置圖片背景資源
第二步,設(shè)置得到圖片背景的draw
第三步,設(shè)置draw參數(shù),并start()
實現(xiàn)效果如下,間隔0.2秒即換圖,實現(xiàn)老電影動畫效果



以上就是Android 逐幀動畫的實例詳解,如有疑問請留言或到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Kotlin自定義實現(xiàn)支付密碼數(shù)字鍵盤的方法實例
這篇文章主要給大家介紹了關(guān)于Kotlin如何自定義實現(xiàn)支付密碼數(shù)字鍵盤的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Android Studio時間選擇器的創(chuàng)建方法
這篇文章主要為大家詳細介紹了Android Studio時間選擇器的創(chuàng)建方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10

