1. 程式人生 > >幀動畫顯示

幀動畫顯示

MainActivity

public class MainActivity extends Activity implements OnClickListener{

private ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.button1).setOnClickListener(this);
    findViewById(R.id.button2).setOnClickListener(this);
    image = (ImageView)findViewById(R.id.imageView1);
    
}

@Override
public void onClick(View v) {
	// TODO Auto-generated method stub
	switch (v.getId()) {
	case R.id.button1:
		//xml動畫
		AnimationDrawable    aa = (AnimationDrawable)image.getBackground();
		aa.start();
		
		break;
	case R.id.button2:
		//java幀動畫
		//建立幀動畫
		AnimationDrawable an = new AnimationDrawable();
		//設定幀(引數1 圖片  引數2 這張圖片在螢幕上顯示多久)
		an.addFrame(getResources().getDrawable(R.drawable.gua1), 20);
		an.addFrame(getResources().getDrawable(R.drawable.gua2), 20);
		an.addFrame(getResources().getDrawable(R.drawable.gua3), 20);
		an.addFrame(getResources().getDrawable(R.drawable.gua4), 20);
		an.addFrame(getResources().getDrawable(R.drawable.gua5), 20);
		an.addFrame(getResources().getDrawable(R.drawable.gua6), 20);
		an.addFrame(getResources().getDrawable(R.drawable.gua7), 20);
	
		image.setImageDrawable(an);
		
		an.start();
		
		break;

	default:
		break;
	}
}

}

activity_main.xml

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="xml幀動畫" />

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="java幀動畫" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@anim/frame_anim" />

建立anim資料夾 在資料夾下面建立anim.xml

<?xml version="1.0" encoding="utf-8"?>

<animation-list
xmlns:android=“http://schemas.android.com/apk/res/android
android:oneshot=“true”

  >
 <!-- 
 這裡的android:oneshot是設定動畫是否只顯示播放一次
 true 只播放一次  false迴圈播放
  --> 
<!--android:drawable="drawble/gua"  圖片  -->
<!--duration ="80" 在介面上展示多少毫秒  -->

<item 
    android:drawable="@drawable/gua1"
    android:duration="80"
    />
    
    


<item
    android:drawable="@drawable/gua2"
    android:duration="80"/>
<item
    android:drawable="@drawable/gua3"
    android:duration="80"/>
<item
    android:drawable="@drawable/gua4"
    android:duration="80"/>



在這裡插入圖片描述