標籤:android style blog http io color ar java sp
天行健,君子以自強不息。——《周易·乾·象》
本講內容:逐幀動畫 Frame Animation
逐幀動畫 Frame Animation就是說一幀一幀的連起來播放就變成了動畫,和放電影的機制很相似。
我們通過一個例子感受一下,代碼的講解都寫在注釋裡了
下面是res/layout/activity_main.xml 布局檔案:
<LinearLayout 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" android:orientation="vertical" tools:context="com.example.text.MainActivity$PlaceholderFragment" > <ImageView android:id="@+id/frame_image" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/start" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="runFrame"/> <Button android:id="@+id/stop" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="stopFrame"/></LinearLayout>
下面是建立的res/anim/frame.xml檔案
<?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/b1" android:duration="300" /> <item android:drawable="@drawable/b2" android:duration="300" /> <item android:drawable="@drawable/b3" android:duration="300" /> <item android:drawable="@drawable/b4" android:duration="300" /> </animation-list>
b1、b2、b3、 b4是四張不同小狐狸表徵圖
下面是MainActivity.java主介面檔案:
public class MainActivity extends Activity implements OnClickListener {private Button start;private Button stop;private ImageView iv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv = (ImageView) findViewById(R.id.frame_image);start = (Button) findViewById(R.id.start);stop = (Button) findViewById(R.id.stop);start.setOnClickListener(this);stop.setOnClickListener(this);iv.setBackgroundResource(R.anim.frame);}@Overridepublic void onClick(View v) {AnimationDrawable anim = (AnimationDrawable) iv.getBackground();switch (v.getId()) {case R.id.start:// 調用動畫可畫對象的開始播放方法anim.start();break;case R.id.stop:// 調用動畫可畫對象的停止播放方法anim.stop();break;}}}
下面是運行結果:
本講到這裡,謝謝大家!
第三十講:Android之Animation(五)