1.代码中的应用。
public class MainActivity extends Activity {
private ImageView im;
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im = (ImageView) findViewById(R.id.im);
}
//开始动画的按钮
@SuppressLint("NewApi")
//在点击事件中实现动画效果
public void jump(View v){
animationDrawable = new AnimationDrawable();
//
animationDrawable.addFrame(getResources().getDrawable(R.drawable.g1), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.g2), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.g3), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.g4), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.g5), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.g6), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.g7), 2000);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.g8), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.g9), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.g10), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.g11), 200);
//给imageView 设置展示动画的资源
im.setBackground(animationDrawable);
animationDrawable.start();
}
public void jump1(View v){
animationDrawable.stop();
}
}
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView im;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im = (ImageView) findViewById(R.id.im);
}
public void jump(View v){
im.setBackgroundResource(R.drawable.wwwww);
AnimationDrawable background = (AnimationDrawable) im.getBackground();
background.start();
}
}