Drawable Animation

Drawable Animation允许你定义一组Drawable资源来创建动画效果,即由Drawable资源组成的帧动画,一般使用一组图片来,通过定义播放行为属性顺序播放图片资源来实现动画效果。AnimationDrawable用来处理用户定义的Drawable Animation。用户可以把Drawable Animation定义为Drawable并放在 res/drawable/ 目录下,与之定义的xml标签是<animation-list>。官方给出的标准定义如下:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

其中:

  • android:oneshot
    循环播放标记,true:循环播放,false:播放一次并停在最后一帧

  • android:duration
    duration用来设置该帧动画的持续时间

如何在代码中使用该动画资源呢,官方给出的示例代码如下

AnimationDrawable rocketAnimation;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
  rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
  rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}

public boolean onTouchEvent(MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_DOWN) {
    rocketAnimation.start();
    return true;
  }
  return super.onTouchEvent(event);
}

另外,官方建议我们不要再onCreate中执行动画的start方法,因为此时AnimationDrawable还没有真正的被加载到窗口中来。我们可以放在用户点击某个按钮时,或者是放到Activity的onWindowFocusChanged()方法中。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容