android 不能播放animation-list动画

经测试,应该是3.0一下的机型在播放animation-lis时,动画没有播放;然后网络搜索,找到一篇文章,讲的非常详细http://www.cnblogs.com/firecode/archive/2012/11/01/2749774.html

里面给出了几种执行动画的方法,我这边就再重复一下:

首先提一点需要注意:

1》ImageView的src与background有什么区别呢?
用src的时候,应是原图显示,不该变图片的大小;用background的时候,按照组件的大小来放大或者缩小图片。

2》代码中使用setBackgroundResource方法设置imageView资源背景,相当于布局文件中的android:background属性,这个属性是view类的属性,必须通过getBackground()方法来获取;而getdrawable()是imageview类的方法,必须通过在代码中setImageResource(int)(对应布局文件的android:src)或setImageDrawable(Drawable drawable)方法设置才可以使用getdrawable()方法;否则程序中代码虽然不会出错,但是运行后会报空指针异常!!!

方法 1 

<ImageView
        android:id="@+id/splash_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="55dp"
        android:src="@drawable/explosiontext" />

iv = (ImageView) findViewById(R.id.splash_iv);
        anim = (AnimationDrawable) iv.getDrawable();
        iv.post(new Runnable() {
            @Override
            public void run() {
                anim.start();
            }
        });

方法 2

// @Override
    // public void onWindowFocusChanged(boolean hasFocus) {
    // anim.start();
    // super.onWindowFocusChanged(hasFocus);
    // }

方法 3 

iv = (ImageView) findViewById(R.id.splash_iv);
        anim = (AnimationDrawable) iv.getDrawable();
        iv.getViewTreeObserver().addOnPreDrawListener(preDrawListener);

OnPreDrawListener preDrawListener = new OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            anim.start();
            return true; // 必须要有这个true返回
        }
    };

方法 4 

imageView.setBackgroundResource(R.anim.xxxxx);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
RunAnim runAnim=new RunAnim();
runAnim.execute("");

class RunAnim extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            if (!animationDrawable.isRunning()) {
                animationDrawable.stop();
                animationDrawable.start();
            }
            return "";
        }
}

 

转载于:https://www.cnblogs.com/lsh-android/p/3375525.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
animation-listAndroid中的一个Drawable资源类型,用于定义逐帧动画。它可以让你定义一系列不同的Drawable资源,并通过设置每个Drawable资源在动画中的时长和顺序来创建一个动画序列。每个Drawable资源可以是一个静态图像、一个逐帧动画或一个过渡动画。你可以将animation-list应用于任何View或ViewGroup的背景,以创建动态的背景效果。 要使用animation-list,你需要在res/drawable文件夹下创建一个XML文件,并在其中定义Drawable资源。以下是一个简单的animation-list示例: ```xml <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <item android:drawable="@drawable/frame1" android:duration="100" /> <item android:drawable="@drawable/frame2" android:duration="100" /> <item android:drawable="@drawable/frame3" android:duration="100" /> </animation-list> ``` 在这个例子中,我们定义了一个animation-list,其中包含三个Drawable资源:frame1、frame2和frame3。我们设置了每个Drawable资源在动画序列中的时长为100毫秒,并将oneshot属性设置为true,表示动画只会播放一次。要在代码中使用这个animation-list,可以通过以下方式: ```java ImageView imageView = findViewById(R.id.image_view); imageView.setBackgroundResource(R.drawable.my_animation); AnimationDrawable anim = (AnimationDrawable) imageView.getBackground(); anim.start(); ``` 在这个例子中,我们首先获取了一个ImageView,并将其背景设置animation-list资源my_animation。然后我们获取ImageView的背景,并将其转换为AnimationDrawable对象。最后,我们调用start()方法开始动画
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值