安卓:幀動畫
阿新 • • 發佈:2018-11-11
一.實現效果
二、涉及知識點
1、活動(Activity)
2、按鈕(Button)
3、影象檢視(ImageView)
4、動畫可繪製物件(AnimationDrawable)
三.實現步驟
1.介面佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/anim" android:src="@drawable/animation_list" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/bottom1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="開始" android:onClick="onStartA"/> <Button android:id="@+id/bottom2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止" android:onClick="onStopA"/> </LinearLayout> </LinearLayout>
新增兩個按鈕來對動畫進行控制
2.繪製動畫
在drawable裡新增圖片
建立animation_list繪製動畫
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/fat_po_f01" android:duration="50"/> <item android:drawable="@drawable/fat_po_f02" android:duration="50"/> <item android:drawable="@drawable/fat_po_f03" android:duration="50"/> <item android:drawable="@drawable/fat_po_f04" android:duration="50"/> <item android:drawable="@drawable/fat_po_f05" android:duration="50"/> <item android:drawable="@drawable/fat_po_f06" android:duration="50"/> <item android:drawable="@drawable/fat_po_f07" android:duration="50"/> <item android:drawable="@drawable/fat_po_f08" android:duration="50"/> <item android:drawable="@drawable/fat_po_f09" android:duration="50"/> <item android:drawable="@drawable/fat_po_f10" android:duration="50"/> <item android:drawable="@drawable/fat_po_f11" android:duration="50"/> <item android:drawable="@drawable/fat_po_f12" android:duration="50"/> <item android:drawable="@drawable/fat_po_f13" android:duration="50"/> <item android:drawable="@drawable/fat_po_f14" android:duration="50"/> <item android:drawable="@drawable/fat_po_f15" android:duration="50"/> <item android:drawable="@drawable/fat_po_f16" android:duration="50"/> <item android:drawable="@drawable/fat_po_f17" android:duration="50"/> <item android:drawable="@drawable/fat_po_f18" android:duration="50"/> <item android:drawable="@drawable/fat_po_f19" android:duration="50"/> <item android:drawable="@drawable/fat_po_f20" android:duration="50"/> <item android:drawable="@drawable/fat_po_f21" android:duration="50"/> <item android:drawable="@drawable/fat_po_f22" android:duration="50"/> <item android:drawable="@drawable/fat_po_f23" android:duration="50"/> <item android:drawable="@drawable/fat_po_f24" android:duration="50"/> <item android:drawable="@drawable/fat_po_f01" android:duration="50"/> <item android:drawable="@drawable/fat_po_f01" android:duration="50"/> <item android:drawable="@drawable/fat_po_f01" android:duration="50"/> </animation-list>
其中duration=“50“是指圖片間的間隔時間
3.MainActivity
package net.hw.donghua; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; private Button button1; private Button button2; private Handler handler; private int n=0; private AnimationDrawable anim; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = findViewById(R.id.anim); button1 = findViewById(R.id.bottom1); button2 = findViewById(R.id.bottom2); iv.setImageResource(R.drawable.animation_list); //載入動畫資原始檔 anim = (AnimationDrawable)iv.getDrawable(); } public void onStartA(View v) { anim.start(); } public void onStopA(View v) { anim.stop(); } }
設定號播放和暫停,在主介面資原始檔裡設定點選,當我們點選開始就能播放動畫了