Drawable Animation

Drawable animation是什么?

  Drawable animation是一种动画类型。

Drawable animation的用途?

  Drawable animation可以用来以你指定的顺序播放一组图片。可以循环播放,也可以只播放一次。

Drawable animation怎么使用?

  Drawable animation可以在任何View上面使用,首先用AnimationDrawable对象设置View的Background,然后调用AnimationDrawable对象的start()方法开始动画。
具体步骤如下:

1.用AnimationDrawable对象设置View的Background,有三种方式:

  • [1].通过调用View的setBackgroundResource(R.drawable.mydrawable)方法设置View的Background
  • [2].通过设置View的XML属性and(和谐)roid:background="@drawable/mydrawable"

    <?xml version="1.0" encoding="utf-8"?>
    <!-- 方式[1]、[2]用到的 mydrawable.xml -->
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item
        android:drawable="@drawable/first"
        android:duration="300"/>
    <item
        android:drawable="@drawable/second"
        android:duration="300"/>
    <item
        android:drawable="@drawable/third"
        android:duration="300"/>
    </animation-list>
  • [3].通过代码创建AnimationDrawable对象,用其设置View的Background
//方式[3]用到的代码
AnimationDrawable animationDrawable = new AnimationDrawable();
animationDrawable.addFrame(getResources().getDrawable(R.drawable.first),300);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.second),300);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.third),300);
animationDrawable.setOneShot(false);//设置循环一次就停止为false,即循环播放
imageView.setBackgroundDrawable(animationDrawable);

2.用AnimationDrawable对象的start()方法

((AnimationDrawable)imageView.getBackground()).start();

注意事项

SDK文档里面有这么一段话

It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus.

因此我们不能在Activity的onCreate()方法里面调用View的start(),而是在Activity的onWindowFocusChanged()方法里面调用View的start()

运行效果图:

XMLDrawableAnimationCodeDrawableAnimation

源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值