帧动画的类
@SuppressLint("NewApi") public class MainActivity extends Activity implements OnClickListener{
private ImageView img;
private Button jq;
private Button jr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.imageView1);
jq=(Button)findViewById(R.id.button1);
jr=(Button)findViewById(R.id.button2);
jq.setOnClickListener(this);
jr.setOnClickListener(this);
}
@Override
public void onClick(View v) {
AnimationDrawable animation;
switch (v.getId()) {
case R.id.button1:
//setBackground方法需要API16现在是8,需要解决 @SuppressLint("NewApi")
img.setBackground(getResources().getDrawable(R.drawable.draw));
animation = (AnimationDrawable) img.getBackground();
animation.start();
break;
case R.id.button2:
img.setBackground(getResources().getDrawable(R.drawable.jrdraw));
animation=(AnimationDrawable) img.getBackground();
animation.start();
break;
default:
break;
}
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<!-- oneshot 默认为false 发射多次 -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
<item android:drawable="@drawable/w0" android:duration="200"/>
<item android:drawable="@drawable/wif0" android:duration="200"/>
<item android:drawable="@drawable/wif1" android:duration="200"/>
<item android:drawable="@drawable/wif2" android:duration="200"/>
<item android:drawable="@drawable/wif4" android:duration="200"/>
<item android:drawable="@drawable/wif5" android:duration="200"/>
</animation-list>
java代码添加动画
@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener {
private ImageView img;
private Button stop;
private Button start;
int draw[] = { R.drawable.p39_d21, R.drawable.p39_d22, R.drawable.p39_d23,
R.drawable.p39_d24, R.drawable.p39_d25, R.drawable.p39_d26,
R.drawable.p39_d27, R.drawable.p39_d28, R.drawable.p39_d29,
R.drawable.p39_d30 };
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
private void init() {
img = (ImageView) findViewById(R.id.imageView1);
start = (Button) findViewById(R.id.button1);
stop = (Button) findViewById(R.id.button2);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
animationDrawable = new AnimationDrawable();
for (int j = 0; j < draw.length; j++) {
.AnimationDrawable.addFrame(Drawable frame, int duration)
animationDrawable
.addFrame(getResources().getDrawable(draw[j]), 50);
}
animationDrawable.setOneShot(false);
img.setBackground(animationDrawable);
animationDrawable.start();
break;
case R.id.button2:
if (animationDrawable.isRunning()) {
animationDrawable.stop();
}
break;
default:
break;
}
}
}