Android View动画之——Frame动画

Drawable Animation,Drawable动画也叫Frame动画,是加载一系列的drawable资源然后逐帧地显示出来的动画,就像放幻灯片一样,其实Drawable Animation也属于View Animation。

Frame Animation使用

Frame Animation也可以使用Java代码方式和xml两种方式,但是推荐使用xml方式。

xml方式

首先在资源文件res/drawable目录下新建一个文件,例如 drawable_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"
    android:variablePadding="false">
    <item
        android:drawable="@drawable/f0"
        android:duration="100" />
    <item
        android:drawable="@drawable/f1"
        android:duration="100" />
    <item
        android:drawable="@drawable/f2"
        android:duration="100" />
    <item
        android:drawable="@drawable/f3"
        android:duration="100" />
    <item
        android:drawable="@drawable/f4"
        android:duration="100" />

</animation-list>

animation-list为根元素,以item表示要轮换显示的图片,duration属性表示各项显示的时间。
android:oneshot=”false” 是循环播放,为true的话则播放到最后一张图片就会停止播放;
android:variablePadding=”false”此属性的意思是padding是否随着Drawable的变化而变化,默认值是false,不建议开启此选项。

播放动画

    ImageView imageView;
    AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setBackgroundResource(R.drawable.drawable_animation);
        animationDrawable = (AnimationDrawable) imageView.getBackground();
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animationDrawable.start();
            }
        });
    }

注意
最好不要在onCreate()方法里开始动画, 因为AnimationDrawable 可能还没有完全附着在Window上,如果你要马上执行动画可以在 onWindowFocusChanged()里开始动画。

Java代码方式

        imageView = (ImageView) findViewById(R.id.imageView);
        animationDrawable = new AnimationDrawable();
        Drawable drawable1 = getResources().getDrawable(R.drawable.f0);
        Drawable drawable2 = getResources().getDrawable(R.drawable.f1);
        Drawable drawable3 = getResources().getDrawable(R.drawable.f2);
        Drawable drawable4 = getResources().getDrawable(R.drawable.f3);
        Drawable drawable5 = getResources().getDrawable(R.drawable.f4);
        animationDrawable.addFrame(drawable1,100);
        animationDrawable.addFrame(drawable2,100);
        animationDrawable.addFrame(drawable3,100);
        animationDrawable.addFrame(drawable4,100);
        animationDrawable.addFrame(drawable5,100);
        imageView.setBackgroundDrawable(animationDrawable);
        animationDrawable.start();

看看效果:
drawable动画
这个人好像没穿衣服

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值