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"/>