Android Animation 动画(二)--Drawable动画

Tween动画的在此http://blog.csdn.net/weixin_37782995/article/details/60782704

Drawable动画:帧动画

这个比较简单,首先你需要一些图片,然后将图片放入drawable文件夹下,再在drawable文件夹下创建xml文件:anim

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item
        android:drawable="@drawable/caomei"
        android:duration="100"
        />
    <item android:drawable="@drawable/fanshiliu"
        android:duration="10"/>
    <item android:drawable="@drawable/ganzhe"
        android:duration="50"/>
    <item android:drawable="@drawable/hongmaodan"
        android:duration="60"/>
    <item android:drawable="@drawable/juzi"
        android:duration="80"/>
    <item android:drawable="@drawable/ningmeng"
        android:duration="200"/>
    <item android:drawable="@drawable/xiangjiao"
        android:duration="1000"/>


</animation-list>

标签改为animation-list

oneshot:表示播放的次数,取值为boolean类型。true代表播放一次,false表示无限循环

item里面的2个参数,一个是图片,一个是显示时长(单位毫秒)


布局一个button 一个imageView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="whh.animationdrawable.MainActivity">
    <Button
        android:id="@+id/btn_anim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="animation"/>
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="center"/>
</LinearLayout>


代码:

public class MainActivity extends AppCompatActivity {
    private Button btn;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn= (Button) findViewById(R.id.btn_anim);
        imageView= (ImageView) findViewById(R.id.imageView);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.btn_anim:
                        imageView.setBackgroundResource(R.drawable.anim);
                        AnimationDrawable anim= (AnimationDrawable) imageView.getBackground();
                        anim.start();
                        break;
                }
            }
        });

    }
}


就3行,

给imageView背景资源值

将imageView的背景资源赋给帧动画anim

anim启动

这里需要注意一下,之前有人遇到停在第一张图不动,网上有资料说是AnimationDrawable不能放在onCreate()里,我试过放在onCreate()里(当然是没有button那种)可以。但是因为我的是6.0机子不能确定低版本的如何所以大家要注意。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值