我正在制作一个当按钮被按下时旋转的物体,我想让它旋转360度,然后继续旋转一个随机数,这样每次它都会落在不同的地方。这是我的xml文件(称为动画)中的内容,它完美地旋转了360。
<rotate
android:fromDegrees = "0"
android:toDegrees = "360"
android:pivotX = "50%"
android:pivotY = "50%"
android:startOffset = "0"
android:duration = "1000" />
我只需要帮助生成它的随机值背后的逻辑。
这也是它在我的java中的显示方式。
but_spin = (Button) findViewById(R.id.spin_but);
final Context mcontext = this;
but_spin.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
ImageView animated = (ImageView) findViewById(R.id.big_button);
anime = AnimationUtils.loadAnimation(mcontext, R.anim.anime);
animated.startAnimation(anime);
}}
);
发布于 2012-11-08 10:20:40
您不能在XML中做到这一点。手动编写动画代码,
static final Random R = new Random(System.currentTimeMillis());
...
Animation a = new RotateAnimation(0, 360 + R.nextInt(180));
ImageView animated = (ImageView) findViewById(R.id.big_button);
animated.startAnimation(a);
有关详细信息,请参阅RotateAnimation
接口文档。
发布于 2012-11-08 10:15:34
您不能在XML文件中生成随机数。
从代码创建一个RotateAnimation。
发布于 2012-11-08 10:19:00
XML布局文件包含在应用程序执行期间不会更改的静态数据。您将需要使用Java代码来生成一个随机数,并根据该值旋转您的可绘制图形。
https://stackoverflow.com/questions/13281384
复制相似问题