Mario Landa
Mario Landa

Reputation: 17

Rotate an object in a circular way using buttons in android studio

I want to make a game which requires several objects to rotate around the center of the screen when you press a button ( go left or right ), however they need to orbit the center periodically as you press the button, not move when you press it once. I can make them move in that way but they do a 360 spin or go back to their original position. Can someone give me an example of how to do this? Its the same functionality as in the circle pong app. Thank you!

Upvotes: 0

Views: 2062

Answers (3)

Zar E Ahmer
Zar E Ahmer

Reputation: 34370

There are multiple ways to do this. If you want to do it using your own Custom View. In computer graphics coordinate system (0,0) start from left top corner. For that first translate your view to center.

canvas.translate(getWidth()/2,getHeight()/2);

then in order to move an object you have to use a mathematical formula.

x = R * cos@
y = R * sin@   where range of cos and sin is between 1 and -1.

R is radius . you can simply draw your view to x,y location . So it will display as it is moving.

and there are other simpler ways. See here

And also see a Custom knob I created at Here

Upvotes: 1

Debu
Debu

Reputation: 615

Maybe this post will help you Move ImageView elliptically around a center in a RelativeView

Here instead of 200,400 put your own diameter based on which you want to rotate the objects

Upvotes: 0

SaravInfern
SaravInfern

Reputation: 3388

In your anim folder create an xml

<?xml version="1.0" encoding="UTF-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:duration="1200" />

in your code

view.startAnimation( 
    AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely) );

Upvotes: 1

Related Questions