如何在XML文件中生成随机数?

far*_*goh 1 android rotation

我正在制作一个按下按钮时旋转的物体,我希望它旋转360度然后继续旋转一个随机数,这样它每次都会落在不同的地方.这就是我在我的xml文件(称为动漫)中所拥有的,它完美地旋转360.

    <rotate 
    android:fromDegrees = "0"
    android:toDegrees = "360"
    android:pivotX = "50%"
    android:pivotY = "50%"
    android:startOffset = "0"
    android:duration = "1000" />
Run Code Online (Sandbox Code Playgroud)

我只需要帮助生成随机值的逻辑.

这也就是它在我的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);  
      }}
    );
Run Code Online (Sandbox Code Playgroud)

Jef*_*man 6

你不能用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);
Run Code Online (Sandbox Code Playgroud)

RotateAnimation有关详细信息,请参阅API文档.