Skip to content

Instantly share code, notes, and snippets.

@gram7gram
Created June 28, 2017 13:37
Show Gist options
  • Save gram7gram/13297a59240fa0ab6af1f40858e9a4fc to your computer and use it in GitHub Desktop.
Save gram7gram/13297a59240fa0ab6af1f40858e9a4fc to your computer and use it in GitHub Desktop.
Android ImageView rotate around pivot point
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);
}
}
@gram7gram
Copy link
Author

ezgif com-optimize 1

@deven4
Copy link

deven4 commented Apr 26, 2021

How can I change the axis of rotation(the pivot point)? I want it to rotate upside down. Please help

@gram7gram
Copy link
Author

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

@deven4
Copy link

deven4 commented Apr 27, 2021

Thanks man.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment