如何在Android中随机旋转图像?

How to randomly rotate an image in Android?

我是 Android 编程的新手,我想知道如何以随机角度旋转图像。我的 "Spin the Bottle" 游戏需要这个。到目前为止,这是我的代码:

package example.com.bottlegame;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.RotateDrawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import java.lang.Object;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import java.util.Random;


public class MainActivity extends ActionBarActivity {

ImageView spin,bottle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    spin = (ImageView) findViewById(R.id.ivSpin);
    bottle = (ImageView) findViewById(R.id.ivBottle);

    final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.roatate);

    spin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            bottle.startAnimation(animRotate);

        }

    });

}

}

现在我正在用 XML 旋转图像,但是你能帮我用 Java 代码旋转它吗?这是 anim 文件夹中的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:drawable="@drawable/bottle"
    android:duration="1000"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:startOffset="0"
/>

</set>` 

这里是 activity_main.xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/bg_background">


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="100sp"
    android:layout_centerHorizontal="true"
    android:id="@+id/ivSpin"
    android:src="@drawable/spin_up"
    android:layout_marginTop="400sp"
    android:contentDescription="@string/spin"
/>

<ImageView
    android:layout_width="65sp"
    android:layout_height="200sp"
    android:id="@+id/ivBottle"
    android:src="@drawable/bottle"
    android:layout_marginTop="80dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/bottle"
/>


</RelativeLayout>

您可以在 Java 代码和 xml 中轻松配置动画。对于您的情况,您可以使用 class RotateAnimation 及其构造函数 public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)

所以,尝试这样的事情:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    spin = (ImageView) findViewById(R.id.ivSpin);
    bottle = (ImageView) findViewById(R.id.ivBottle);

    float toDegrees = new Random().nextFloat() * Integer.MAX_VALUE % 360;
    final Animation animRotate = new RotateAnimation(0, toDegrees, 50, 50);
    animRotate.setDuration(1000);
    animRotate.setRepeatCount(1);
    animRotate.setRepeatMode(Animation.REVERSE);

    spin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bottle.startAnimation(animRotate);
        }
    });
}

我认为,不需要评论。