如何即时更改Android AnimationDrawable动画的持续时间?

我是ANDROID平台的新手.我在使用AminationDrawable在我的应用程序中使用以下动画设置一组16“框架:

XML文件我有

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"  android:oneshot="false">
<item android:drawable="@drawable/image_1" android:duration="200" />
<item android:drawable="@drawable/image_1_25" android:duration="200" /> 
<item android:drawable="@drawable/image_1_5" android:duration="200" />
<item android:drawable="@drawable/image_1_75" android:duration="200" /> 
<item android:drawable="@drawable/image_2" android:duration="200" />
<item android:drawable="@drawable/image_2_25" android:duration="200" /> 
<item android:drawable="@drawable/image_2_5" android:duration="200" />
<item android:drawable="@drawable/image_2_75" android:duration="200" />
<item android:drawable="@drawable/image_3" android:duration="200" />
<item android:drawable="@drawable/image_3_25" android:duration="200" /> 
<item android:drawable="@drawable/image_3_5" android:duration="200" />
<item android:drawable="@drawable/image_3_75" android:duration="200" />
<item android:drawable="@drawable/image_4" android:duration="200" />
<item android:drawable="@drawable/image_4_25" android:duration="200" /> 
<item android:drawable="@drawable/image_4_5" android:duration="200" />
<item android:drawable="@drawable/image_4_75" android:duration="200" /> 
</animation-list>

在Java代码中,我有以下几个

首先我要声明该类并添加一个onCreate()方法,其中我设置了动画.

public class MyNewActivity extends Activity 
{
    // member variables (accessible from within class methods below).
    AnimationDrawable mainAnimation;
    long mSpeed = 50;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_widget);
        // set up image
        ImageView mainImage = (ImageView) findViewById(R.id.image_widget);
        mainImage.setBackgroundResource(R.drawable.animated_image);
        mainAnimation = (AnimationDrawable) mainImage.getBackground();
    };
<...snip...>

…然后我稍后在用户按下一个按钮的时候开始画图,我打电话给以下动画开始动画:

private void start()
{
    // start the image rotating.
    if (mainAnimation.isRunning())
        mainAnimation.stop();

    int numFrames = mainAnimation.getNumberOfFrames();
    for (int ii = 0; ii < numFrames; ii++ )
    {
        // change the animation speed.
        Drawable d = mainAnimation.getFrame(ii);
        mainAnimation.scheduleDrawable(d,mainAnimation,mSpeed);
    }
}

< …剪断…>

所以代码中的其他地方我有一个地方来调整成员变量“mSpeed”.如果我这样做,然后调用“start()”,动画就会启动,但速度总是相同的(基本上是上面的XML定义的),我的问题是如何修改帧的“持续时间”基于用户输入来移动这个动画更快/更慢吗?我看不到修改“持续时间”状态的方法,而且假设上面的“ScheduleDrawable()”调用将改变图形的帧持续时间.

谢谢您的帮助
标记.

解决方法

我有同样的问题,但是当我通过实现我自己的AnimationDrawable类来扩展AnimationDrawable并且重写Run()方法添加setDuration()方法,我可以通过实现我的AnimationDrawable source code来设法解决它,它允许我设置如下的持续时间:

通过查看原始运行方法,我们看到它执行相同,但调用scheduleSelf(这是SystemClock.uptimeMillis()持续时间);在添加框架时指定的持续时间,以便我更改它.我也添加时间,因为我使用相同的所有我的框架,但你可以使用新的持续时间的数组.

import android.graphics.drawable.AnimationDrawable;
import android.os.SystemClock;

public class MyAnimationDrawable extends AnimationDrawable {

    private volatile int duration;//its volatile because another thread will update its value
    private int currentFrame;

    public MyAnimationDrawable() {
        currentFrame = 0;
    }

    @Override
    public void run() {
        int n = getNumberOfFrames();
        currentFrame++;
        if (currentFrame >= n) {
            currentFrame = 0;
        }
        selectDrawable(currentFrame);
        scheduleSelf(this,SystemClock.uptimeMillis() + duration);
    }

    public void setDuration(int duration) {
        this.duration = duration;
        //we have to do the following or the next frame will be displayed after the old duration
        unscheduleSelf(this);
        selectDrawable(currentFrame);
        scheduleSelf(this,SystemClock.uptimeMillis()+duration);
    }
}

这是我的第一个答案,所以我希望它可以帮助你,这是很好的解释.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...