简介:
这个例子 展示了一个怎样使用AnimationDrawable 的Demo。
https://www.youtube.com/watch?v=V3ksidLf7vA
ps:最近youtube上面的code下载连接更换了地址,当点击那个连接的时候,会自动跳转到另外的地址,如果下载不成功,请在前面加上https://XXX 即可正常下载。(Goagent)
/**
* This example shows how to use AnimationDrawable to construct a keyframe animation where each
* frame is shown for a specified duration.
*
* Watch the associated video for this demo on the DevBytes channel of developer.android.com
* or on YouTube at https://www.youtube.com/watch?v=V3ksidLf7vA.
*/
public class KeyframeAnimation extends Activity {
AnimationDrawable animationDrawable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_keyframe_animation);
ImageView imageview = (ImageView) findViewById(R.id.imageview);
// Create the AnimationDrawable in which we will store all frames of the animation
//
animationDrawable = new AnimationDrawable();
for (int i = 0; i < 10; ++i) {
animationDrawable.addFrame(getDrawableForFrameNumber(i), 300);
}
// Run until we say stop
animationDrawable.setOneShot(false);
imageview.setImageDrawable(animationDrawable);
// When the user clicks on the image, toggle the animation on/off
imageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (animationDrawable.isRunning()) {
animationDrawable.stop();
} else {
animationDrawable.start();
}
}
});
}
/**
* The 'frames' in this app are nothing more than a gray background with text indicating
* the number of the frame.
* 构造的'frames'是带数字显示 灰色背景显示
*/
private BitmapDrawable getDrawableForFrameNumber(int frameNumber) {
Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.GRAY);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(80);
paint.setColor(Color.BLACK);
canvas.drawText("Frame " + frameNumber, 40, 220, paint);
return new BitmapDrawable(getResources(), bitmap);
}
}
Drawable Animation翻译:
Drawable 动画可以按顺序装载一系列 Drawable 资源,以便创建动画并显示。这是传统的动画方式,把一系列不同的图片按顺序显示出来,就像播放电影胶片一样。 Drawable 动画的基础是 AnimationDrawable 类。
如果要用代码来定义动画的各个帧,请使用 AnimationDrawable 类的 API。不过更简便的方式是用一个 XML 文件来实现,其中列出了组成动画的所有帧。 这个 XML 文件位于 Android 项目的 res/drawable/ 目录下,内含了每个动画帧的显示顺序、持续时间等定义。
XML 文件由一个 <animation-list> 根元素和一系列 <item> 子节点组成,每个子节点定义了一个帧:一个作为帧显示的 drawable 资源和显示的持续时间。 以下是 Drawable 动画 XML 文件的一个实例:
这个动画只显示了三帧。android:oneshot 属性设置为 true表示只播放一次,然后停止并保持显示于最后一帧。 如果该属性设为 false,则动画将会循环播放。 这段 XML 保存于项目 res/drawable/ 目录下的 rocket_thrust.xml,可加到 View 的背景图片上并显示出来。 下面是一个 Activity 的示例,动画加到一个 ImageView 上,当被触摸到时显示动画:
有一点非常重要,请注意在 Activity 的 onCreate() 方法中,不能调用 AnimationDrawable 的 start() 方法。 因为此时 AnimationDrawable 还没有完全装入窗口。如果你需要立即显示动画,而不需要用户交互,你可能需要在 Activity 的 onWindowFocusChanged() 方法中调用,窗口获得焦点时 Android 系统将会调用该方法。
有关 XML 语法的详情、支持的标记和属性,请参阅 Animation Resources 。
如果要用代码来定义动画的各个帧,请使用 AnimationDrawable 类的 API。不过更简便的方式是用一个 XML 文件来实现,其中列出了组成动画的所有帧。 这个 XML 文件位于 Android 项目的 res/drawable/ 目录下,内含了每个动画帧的显示顺序、持续时间等定义。
XML 文件由一个 <animation-list> 根元素和一系列 <item> 子节点组成,每个子节点定义了一个帧:一个作为帧显示的 drawable 资源和显示的持续时间。 以下是 Drawable 动画 XML 文件的一个实例:
<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,则动画将会循环播放。 这段 XML 保存于项目 res/drawable/ 目录下的 rocket_thrust.xml,可加到 View 的背景图片上并显示出来。 下面是一个 Activity 的示例,动画加到一个 ImageView 上,当被触摸到时显示动画:
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);
}
有一点非常重要,请注意在 Activity 的 onCreate() 方法中,不能调用 AnimationDrawable 的 start() 方法。 因为此时 AnimationDrawable 还没有完全装入窗口。如果你需要立即显示动画,而不需要用户交互,你可能需要在 Activity 的 onWindowFocusChanged() 方法中调用,窗口获得焦点时 Android 系统将会调用该方法。
有关 XML 语法的详情、支持的标记和属性,请参阅 Animation Resources 。
参考:
https://developer.android.com/guide/topics/graphics/drawable-animation.html
https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
http://blog.sina.com.cn/s/blog_48d4913001011zdr.html