目前我正在开发我的应用程序的一部分,其中有一个建筑物的 ImageView。根据用户的级别,选择一个随机数作为总生命值,这是从建筑物的健康状况中获取的。
例如,如果用户的级别为 1,则会生成一个 0 到 10 之间的随机数,称为“hitpoints”。我的问题是在 ImageView 上显示这个随机数的动画的最佳方式是什么?
我之前想,我可以创建多个建筑物图像,并在其顶部显示不同的数字,但这需要我制作大约 50 个图像副本,并且会占用太多空间。
那么,有没有一种方法可以在我的 ImageView 顶部显示一个小动画,我可以将这个随机值插入其中?
感谢您的帮助。
最佳答案
你可以将 ImageView 和 TextView 放在 FrameLayout 中:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/building"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_building_image" />
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
至于动画,你还没有指定你想要什么类型的动画(淡入、上/下/左/右滑动等),但你需要做的就是像下面这样您想要为包含数字的 TextView 制作动画:
在 Activity 中为动画创建一个方法(示例是动画淡入淡出):
private Animation fadeInAnimation() {
Animation animation = new AlphaAnimation(0f, 1.0f);
animation.setDuration(1000); // in milliseconds
animation.setFillEnabled(true);
animation.setFillAfter(true);
return animation;
}
...
TextView number = (TextView) findViewById(R.id.number);
通过调用上述方法为您的 TextView 制作动画:
number.setText(yourRandomNumber.toString());
number.startAnimation(fadeInAnimation());
关于java - 在 ImageView 之上显示动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27992777/