Created
June 28, 2017 13:37
-
-
Save gram7gram/13297a59240fa0ab6af1f40858e9a4fc to your computer and use it in GitHub Desktop.
Android ImageView rotate around pivot point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gram.ua.robinzon.listener.interaction.protractor; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.animation.RotateAnimation; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
/** | |
* @author Gram <gram7gram@gmail.com> | |
*/ | |
public class ArrowTouchListener implements View.OnTouchListener { | |
private final String tag = getClass().getSimpleName(); | |
private final int NONE = 0; | |
private final int DRAG = 1; | |
private final TextView rotationLbl; | |
private int mode = NONE; | |
private int prevRotation; | |
public ArrowTouchListener(TextView rotationLbl) { | |
this.rotationLbl = rotationLbl; | |
} | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
final ImageView view = (ImageView) v; | |
switch (event.getAction() & MotionEvent.ACTION_MASK) { | |
case MotionEvent.ACTION_DOWN: | |
onActionDown(event); | |
break; | |
case MotionEvent.ACTION_UP: | |
onActionUp(event); | |
break; | |
case MotionEvent.ACTION_POINTER_UP: | |
onPointerUp(event); | |
break; | |
case MotionEvent.ACTION_MOVE: | |
switch (mode) { | |
case DRAG: | |
onActionMoveDrag(view, event); | |
break; | |
} | |
break; | |
} | |
return true; | |
} | |
private void onActionDown(MotionEvent event) { | |
mode = DRAG; | |
} | |
private void onActionUp(MotionEvent event) { | |
} | |
private void onActionMoveDrag(ImageView view, MotionEvent event) { | |
view.setPivotY((view.getY() + view.getHeight()) * .98f); | |
int angle = getAngle(view.getPivotX(), view.getPivotY(), event.getX(), event.getY()); | |
if (angle < 0) angle = 0; | |
if (angle > 90) angle = 90; | |
int currentRotation = 90 - angle; | |
RotateAnimation rotate = new RotateAnimation(prevRotation, currentRotation, | |
RotateAnimation.RELATIVE_TO_SELF, 0.5f, | |
RotateAnimation.RELATIVE_TO_SELF, 0.98f); | |
rotate.setFillAfter(true); | |
rotate.setDuration(100); | |
view.startAnimation(rotate); | |
prevRotation = currentRotation; | |
rotationLbl.setText(angle + "*"); | |
} | |
private void onPointerUp(MotionEvent event) { | |
mode = NONE; | |
} | |
private int getAngle(float xt, float yt, float x, float y) { | |
float dx = x - xt; | |
float dy = yt - y; | |
double inRads = Math.atan2(dy, dx); | |
return (int) Math.toDegrees(inRads); | |
} | |
} |
Author
gram7gram
commented
Jun 28, 2017
How can I change the axis of rotation(the pivot point)? I want it to rotate upside down. Please help
Welcome! I suppose you need to update these numbers to achieve a needed angle:
if (angle < 0) angle = 0;
if (angle > 90) angle = 90;
int currentRotation = 90 - angle;
Use values like 360/270/180/90/0/-90/-180/-270
until you succeed :)
Thanks man.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment