【问题标题】:How can I change the duration for an Android AnimationDrawable animation on-the-fly?如何动态更改 Android AnimationDrawable 动画的持续时间?
【发布时间】:2010-12-06 21:24:18
【问题描述】:

我是 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);
    }
}
<...snip...>

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

【问题讨论】:

  • 好问题。通过查看开发人员文档,我看不到这样做的方法,但我有兴趣看到答案。

标签: android


【解决方案1】:

我遇到了同样的问题,但我在阅读 AnimationDrawable 的 source code 时设法解决了这个问题,方法是实现我自己的扩展 AnimationDrawable 的 AnimationDrawable 类并覆盖 Run() 方法并添加 setDuration() 方法,该方法允许我设置持续时间如下:

通过查看原始 run 方法,我们看到它的作用相同,但通过调用 scheduleSelf(this, SystemClock.uptimeMillis() + duration); 并使用您在添加框架时指定的持续时间,因此我对其进行了更改。我还添加了持续时间,因为我对所有帧都使用相同的,但您可以使用新持续时间的数组。

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);
    }
}

这是我的第一个答案,所以我希望它对您有所帮助,并且解释得很好。

【讨论】:

  • 感谢您的帮助!这很有道理。
  • 你能不能接受这个答案,这样我才能有更多的声望进入聊天室。
  • 你是如何将AnimationDrawable 类转换为MyAnimationDrawable 的?我遇到了从rocketImage.getBackground() 方法返回的对象的问题。 ` ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image); RocketImage.setBackgroundResource(R.drawable.rocket_thrust); RocketAnimation = (AnimationDrawable) RocketImage.getBackground();`有什么建议吗?
  • 我正在从内存中读取图像,所以我创建了 MyAnimationDrawable 对象并以编程方式添加帧,然后将其设置为 ImageView drawable 像这样 animDrawable = new MyAnimationDrawable(); int n = filesNames.length;可画框; for (int i = 0; i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多