我是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>
Run Code Online (Sandbox Code Playgroud)
在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...>
Run Code Online (Sandbox Code Playgroud)
...然后我在用户按下按钮时开始绘图我调用以下内容开始动画移动:
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...>
Run Code Online (Sandbox Code Playgroud)
所以代码中的其他地方我有一个调整成员变量的地方mSpeed.如果我这样做然后调用start(),动画将开始,但速度总是相同的(基本上是在上面的XML中定义的.我的问题是,如何修改帧的"持续时间"以更快地移动此动画/基于用户输入慢?我看不到修改"持续时间"状态的方法,并且假设ScheduleDrawable()上面的调用会改变绘图的帧持续时间.
Ama*_*mal 20
我有同样的问题,但我通过实现我自己的AnimationDrawable类来扩展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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的第一个答案,所以我希望它对你有所帮助,而且解释得很好.
| 归档时间: |
|
| 查看次数: |
11751 次 |
| 最近记录: |