我怎样才能改变持续时间为一个Android AnimationDrawable动画上的飞?时间为、画上、Android、AnimationDrawable

2023-09-12 09:12:00 作者:小气鬼

我是新来的Andr​​oid平台。我使用的是下面在我的应用程序中使用AminationDrawable动画一套16帧:

I'm new to ANDROID platform. I'm using the following to animate a set of 16 "frames" using AminationDrawable in my app:

在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 code我有以下

首先我声明类和添加的onCreate()方法,在这里我设置了动画。

first I'm declaring the class and adding an onCreate() method where I set up the animation.

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...>

......再后来,我开始我的画,当用户presses一个按钮,我叫下面的启动动画运动:

...then later on I start my drawing when the user presses a button I call the following to start the animation moving:

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

&LT; ...喀嚓...>

<...snip...>

所以其他地方的code我有一个地方调整成员变量MSPEED。如果我这样做,然后调用开始(),动画将开始,但速度始终是相同的(基本上就是在上面的XML定义。我的问题是,我该怎么修改帧的持续时间移动此动画快/慢根据用户的输入?我看不出有什么办法修改了时间的状态,是假设的ScheduleDrawable()拨打以上将改变绘制的帧的持续时间。在

So elsewhere in the code I have a place to adjust the member variable "mSpeed". If I do this and then call "start()", the animation will start, however the speed is always the same (essentially what was defined in the XML above. My question is, how can I modify the "duration" of the frames to move this animation faster/slower based on user input? I see no way to modify a "duration" state, and was under the assumption the "ScheduleDrawable()" call above would change the drawing's frames duration.

感谢您的帮助 马克。

推荐答案

我有同样的问题,但我设法解决它读取数据时的source AnimationDrawable通过实施延伸AnimationDrawable和我自己的AnimationDrawable类code 覆盖运行()方法,并添加 setDuration() 的方法,让我设定的期限如下:

I had the same problem but I managed to solve it when reading the source code of AnimationDrawable by implementing my own AnimationDrawable class that extends AnimationDrawable and override the Run() method and add setDuration() method which allows me to set the duration as follow:

通过回顾,我们看到,它这样做原来的运行方法,但通过调用 scheduleSelf(这一点,SystemClock.uptimeMillis()+时间); 的持续时间您指定添加帧时,所以我改变了它。我还要补充的持续时间,因为我用的是相同的所有我的框架,但你可以使用新的时间序列。

By reviewing the original run method we see that it do the same but by calling scheduleSelf(this, SystemClock.uptimeMillis() + duration); with the duration you specified when adding the frame so I changed it. I also add duration because I use the same for all my frames but you can use array of new 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);
}

}

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

It's my first answer so I hope it helps you and it's explained well.