在 Android 动画期间更改图像
我一直致力于学习 Android 中的动画,并且根据教程,到目前为止相当简单。然而,我想让动画比教程教授的内容更进一步。
我想模拟骰子的滚动。这是我将骰子添加到视图的代码。
<ImageButton android:id="@+id/dice" android:clickable="true"
android:onClick="rollDie" android:src="@drawable/dice_sides"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@null" />
源dice_sides
是一个级别列表。
添加初始动画很容易。在 Activity 的 rollDie
方法中,我执行了以下操作:
/**
* Roll of the die.
*
* @param view
* The view that is being acted upon.
*/
public void rollDie(View view) {
// Setup the animation.
Animation move = AnimationUtils.loadAnimation(view.getContext(),
R.anim.move);
View dice = findViewById(R.id.dice);
dice.setAnimation(move);
move.start();
}
效果很好。模具图像来回晃动。
我想要发生的是骰子在动画中移动时显示各种数字,直到它停在动画的最后部分。不幸的是,我找不到太多关于如何与第一个动画一起制作第二个动画的信息。有人提到了处理程序的使用,但我不太明白在这种情况下如何使用它们。如果有人能指出我正确的方向,我将不胜感激。
(作为记录,这里是 dice_sides。)
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0" android:drawable="@drawable/die.1" />
<item android:maxLevel="1" android:drawable="@drawable/die.2" />
<item android:maxLevel="2" android:drawable="@drawable/die.3" />
<item android:maxLevel="3" android:drawable="@drawable/die.4" />
<item android:maxLevel="4" android:drawable="@drawable/die.5" />
<item android:maxLevel="5" android:drawable="@drawable/die.6" />
</level-list>
I have been working on learning animations in Android, and, based on the tutorials, it's fairly easy so far. However, I wanted to take the animations a step further than what the tutorials teach.
I wanted to simulate the rolling of a die. Here is my code to add the die to the view.
<ImageButton android:id="@+id/dice" android:clickable="true"
android:onClick="rollDie" android:src="@drawable/dice_sides"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@null" />
The source, dice_sides
, is a level-list.
Adding the initial animation was easy. Within the rollDie
method in the Activity, I did this:
/**
* Roll of the die.
*
* @param view
* The view that is being acted upon.
*/
public void rollDie(View view) {
// Setup the animation.
Animation move = AnimationUtils.loadAnimation(view.getContext(),
R.anim.move);
View dice = findViewById(R.id.dice);
dice.setAnimation(move);
move.start();
}
That works fine. The die image shakes back and forth.
What I want to have happen is the die to then display various numbers as it moves through the animation, until it stops on the last part of the animation. Unfortunately, I can't find much on how to do a second animation along with the first. Someone mentioned the use of handlers, but I don't quite understand how to use them in this situation. If anybody could point me in the right direction, I would appreciate it.
(For the record, here is dice_sides.)
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0" android:drawable="@drawable/die.1" />
<item android:maxLevel="1" android:drawable="@drawable/die.2" />
<item android:maxLevel="2" android:drawable="@drawable/die.3" />
<item android:maxLevel="3" android:drawable="@drawable/die.4" />
<item android:maxLevel="4" android:drawable="@drawable/die.5" />
<item android:maxLevel="5" android:drawable="@drawable/die.6" />
</level-list>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我花了一点时间才弄清楚,但我最终确定了如何让动画的两个部分一起工作。 (关于处理程序的提示是正确的。)我写了一篇 博客文章讨论如何让它发挥作用。它可以很容易地推断出更高级的动画。
It took me a little bit to figure it out, but I finally determined how to get both parts of the animation working together. (The tip on handlers was spot on.) I wrote a blog post discussing how to get this to work. It can easily be extrapolated to more advanced animations.