Drawable Animation

一、实现方式

(1)代码中添加实现

  • 将图片添加到AnimationDrawable对象,在将对象添加到imageView中,调用start()方法启动动画。
AnimationDrawable animationDrawable = new AnimationDrawable();
animationDrawable.addFrame(getResources().getDrawable(R.drawable.a), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.b), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.c), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.d), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.e), 200);
animationDrawable.setOneShot(true);//设置是否只播放一次
imageView.setImageDrawable(animationDrawable);
animationDrawable.start();

(2)生成 animation-list 的资源文件,在代码中引用。

  • 在drawable文件下新建abunation_list的xml文件,在文件中添加图片资源。
  • 将xml文件添加到imageView中,通过getDrawable方法获取AnimationDrawable对象,调用start()方法启动动画。


    
    
    
    
    

  • oneshot:设置是否只播放一次,默认为false
  • drawable:设置当前图片资源
  • duration:设置当前图片时间
imageView.setBackgroundResource(R.drawable.anim_list);
animationDrawable = (AnimationDrawable) imageView.getDrawable();
animationDrawable.start();

注意方式:

  • 在onResume方法中添加它,防止发生空指针异常。
  • 不建议添加太大的图片,因为这很容易导致OOM。

你可能感兴趣的:(Drawable Animation)