Android 用Animation-list實現逐幀動畫,android逐幀動畫圖片
我們要實現的效果
準備資源檔放到
res/drawable檔案下中
light01.png
light02.png
light03.png
編寫
loading.xml 放到
res/drawable
<!-- animation-list :根標籤 oneshot :代表著是否只展示一遍,設定為false會不停的迴圈播放動畫 android :duration 表示該圖片的動畫時間長度 --> <?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <item android:drawable="@drawable/light01" android:duration="50"/> <item android:drawable="@drawable/light02" android:duration="50"/> <item android:drawable="@drawable/light03" android:duration="50"/></animation-list>
在
activity_main.xml布局檔案中,這樣寫
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ImageView android:id="@+id/main_img_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5px" android:src="@drawable/loading"/> <Button android:id="@+id/main_bt_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5px" android:text="開始" /> <Button android:id="@+id/main_bt_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5px" android:text="停止" /></LinearLayout>
在Activity中這樣來寫
bt_start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //擷取 AnimationDrawable animationDrawable = (AnimationDrawable) animationIV.getDrawable(); //開始動畫 animationDrawable.start(); } }); bt_stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //擷取 AnimationDrawable animationDrawable = (AnimationDrawable) animationIV.getDrawable(); //停止動畫 animationDrawable.stop(); } });
有興趣的童鞋可以關注我的Blog,我的專欄會持續更新Android Studio 權威教程,以及2015 I/O大會上的NDK的配置和編譯,我也全部會分享給大家。
並且我收到了CSND 的講師邀請,後期我會把這些Android Studio的使用教程錄製成視頻。
/** * -------------- * 歡迎轉載 | 轉載請註明 * -------------- * 如果對你有協助,請點擊|頂| * -------------- * 請保持謙遜 | 你會走的更遠 * -------------- * @author zsl * @github https://github.com/yy1300326388 * @blog http://blog.csdn.net/yy1300326388 */
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。