【问题标题】:Displaying an Animation on top of an ImageView [closed]在 ImageView 上显示动画 [关闭]
【发布时间】:2015-03-15 13:52:51
【问题描述】:
目前,我正在开发我的应用程序的一部分,该部分的 ImageView 是建筑物。根据用户的等级,选择一个随机数作为总生命值,这取自建筑物的健康状况。
例如,如果用户的级别为 1,则会生成一个介于 0 和 10 之间的随机数,称为“命中点”。我的问题是在 ImageView 上显示这个随机数的动画的最佳方式是什么?
我之前在想,我可以创建多个建筑物的图像,所有图像上都显示不同的数字,但这需要我制作大约 50 个图像副本,而且会占用太多空间。
那么,有没有办法在我的 ImageView 上显示一个小动画,我可以将这个随机值插入其中?
感谢您的帮助。
【问题讨论】:
标签:
java
android
animation
random
imageview
【解决方案1】:
你可以将 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());