第一步準備好資源,第二步建立LoadActivity,第三步在anim目錄下建立play_anim.xml檔案
最後一步建立布局檔案。
package com.weixin;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class LoadActivity extends Activity {
private Button btn_load;
private ImageView iv_load;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_anim);
btn_load = (Button) findViewById(R.id.btn_loadanim);
iv_load = (ImageView) findViewById(R.id.image);
}
public void load(View v) {
iv_load.setBackgroundResource(R.anim.play_anim);
AnimationDrawable frameAnimation = (AnimationDrawable) iv_load
.getBackground();
if (frameAnimation.isRunning()) {
// frameAnimation.setLevel(10);
frameAnimation.stop();
} else {
frameAnimation.stop();
// frameAnimation.setLevel(15);
frameAnimation.start();
}
}
};
play_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/v_anim1" android:duration= "300"></item>
<item android:drawable="@drawable/v_anim2" android:duration= "300"></item>
<item android:drawable="@drawable/v_anim3" android:duration= "300"></item>
</animation-list>
最後布局檔案
<?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:background="#F000"
android:orientation="vertical" >
<Button
android:id="@+id/btn_loadanim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="load"
android:text="開啟動畫"
android:textSize="24sp" />
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/v1" />
</LinearLayout>
最後效果
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TN4kTN0ITNzIDMygDM2EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)