类似于电影,一张张图片连续播放
1、xml方式anim.xml
2、代码方式
activity_main.xml
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
imageView.setImageResource(R.drawable.anim);
findViewById(R.id.start).setOnClickListener(v -> {
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getDrawable();
if (animationDrawable != null && !animationDrawable.isRunning()) {
animationDrawable.start();
}
});
findViewById(R.id.stop).setOnClickListener(v -> {
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
if (animationDrawable != null && animationDrawable.isRunning()) {
animationDrawable.stop();
}
});
findViewById(R.id.java_code).setOnClickListener(v -> {
frameAnimation();
});
}
private void frameAnimation() {
AnimationDrawable animationDrawable = new AnimationDrawable();
animationDrawable.addframe(getDrawable(R.drawable.anim1), 100);
animationDrawable.addframe(getDrawable(R.drawable.anim2), 100);
animationDrawable.addframe(getDrawable(R.drawable.anim3), 100);
animationDrawable.addframe(getDrawable(R.drawable.anim4), 100);
animationDrawable.addframe(getDrawable(R.drawable.anim5), 100);
animationDrawable.addframe(getDrawable(R.drawable.anim6), 100);
animationDrawable.addframe(getDrawable(R.drawable.anim7), 100);
animationDrawable.addframe(getDrawable(R.drawable.anim8), 100);
animationDrawable.addframe(getDrawable(R.drawable.anim9), 100);
animationDrawable.addframe(getDrawable(R.drawable.anim10), 100);
animationDrawable.addframe(getDrawable(R.drawable.anim11), 100);
animationDrawable.addframe(getDrawable(R.drawable.anim12), 100);
animationDrawable.setOneShot(true);
imageView.setImageDrawable(animationDrawable);
animationDrawable.start();
}
}
三、补间动画
其中AnimationSet是其余几种以及其他AnimationSet的组合
基本属性:
其中,
Duration:持续时间,单位是毫秒
Interpolator:插值器
插值器列表
示例...
alpha.xml
rorate.xml
scale.xml
translate.xml
activity_main.xml
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
findViewById(R.id.alpha).setOnClickListener(v -> {
alpha();
});
findViewById(R.id.scale).setOnClickListener(v -> {
scale();
});
findViewById(R.id.translate).setOnClickListener(v -> {
translate();
});
findViewById(R.id.rotate).setOnClickListener(v -> {
rotate();
});
findViewById(R.id.animation_set).setOnClickListener(v -> {
animation_set();
});
findViewById(R.id.java_code).setOnClickListener(v -> {
java_code();
});
findViewById(R.id.serial_play).setOnClickListener(v -> {
serial_play();
});
}
private void alpha() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
imageView.startAnimation(animation);
}
private void scale() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
imageView.startAnimation(animation);
}
private void translate() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
imageView.startAnimation(animation);
}
private void rotate() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
imageView.startAnimation(animation);
}
private void animation_set() {
AnimationSet animationSet = new AnimationSet(true);
Animation alpha = AnimationUtils.loadAnimation(this, R.anim.alpha);
Animation scale = AnimationUtils.loadAnimation(this, R.anim.scale);
Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);
Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
animationSet.addAnimation(alpha);
animationSet.addAnimation(scale);
animationSet.addAnimation(translate);
animationSet.addAnimation(rotate);
imageView.startAnimation(animationSet);
}
private void java_code() {
// AlphaAnimation animation = new AlphaAnimation(0, 1);
// ScaleAnimation animation = new ScaleAnimation(1, 2, 1, 2);
// TranslateAnimation animation = new TranslateAnimation(100, 100, 200, 300);
RotateAnimation animation = new RotateAnimation(0, 135);
animation.setDuration(1000);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatMode(Animation.REVERSE);
animation.setRepeatCount(2);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.i("Simon", "onAnimationStart");
}
@Override
public void onAnimationEnd(Animation animation) {
Log.i("Simon", "onAnimationEnd");
}
@Override
public void onAnimationRepeat(Animation animation) {
Log.i("Simon", "onAnimationRepeat");
}
});
imageView.startAnimation(animation);
}
private void serial_play() {
anim1();
}
private void anim1() {
AlphaAnimation animation = new AlphaAnimation(0, 1);
animation.setDuration(1000);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatMode(Animation.REVERSE);
animation.setRepeatCount(2);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.i("Simon", "onAnimationStart");
}
@Override
public void onAnimationEnd(Animation animation) {
Log.i("Simon", "onAnimationEnd");
anim2();
}
@Override
public void onAnimationRepeat(Animation animation) {
Log.i("Simon", "onAnimationRepeat");
}
});
imageView.startAnimation(animation);
}
private void anim2() {
ScaleAnimation animation = new ScaleAnimation(1, 2, 1, 2);
animation.setDuration(1000);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatMode(Animation.REVERSE);
animation.setRepeatCount(2);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.i("Simon", "onAnimationStart");
}
@Override
public void onAnimationEnd(Animation animation) {
Log.i("Simon", "onAnimationEnd");
}
@Override
public void onAnimationRepeat(Animation animation) {
Log.i("Simon", "onAnimationRepeat");
}
});
imageView.startAnimation(animation);
}
}



