jelic98
jelic98

Reputation: 713

How to randomly rotate an image in Android?

I'm new at Android Programing and I want to know how to rotate an image by a random angle. I need this for my "Spin the Bottle" game. Here is my code so far:

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);

        }

    });

}

}

Now I'm rotating an image with XML, but can you help me to rotate it with just Java code. Here is the XML code in anim folder:

<?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>` 

And here is the 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>

Upvotes: 0

Views: 2037

Answers (1)

Vasilov Artur
Vasilov Artur

Reputation: 1487

You can easily configure your animation in Java code as well as in xml. For your case you could use class RotateAnimation and its' constructor public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)

So, try somehting like this:

@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);
        }
    });
}

I think, that no comments are required.

Upvotes: 1

Related Questions