I'm trying to figure out the best way to make an image rotate along with a user's finger dragging it left or right, and the angle of rotation.
float x1 = lastEvent[0] - lastEvent[1];
float y1 = lastEvent[2] - lastEvent[3];
float degrees1 = (float)(Math.atan2(y1, x1));
float x2 = event.getX(0) - event.getX(1);
float y2 = event.getY(0) - event.getY(1);
float degrees2 = (float)(Math.atan2(y2, x2));
float degrees = (float) Math.toDegrees(degrees2-degrees1);
But it doesn't rotate like i want it.. Is there any listener for rotation gesture? Thanks
user430926
/** Determine the degree between the first two fingers */
private float rotation(MotionEvent event) {
double delta_x = (event.getX(0) - event.getX(1));
double delta_y = (event.getY(0) - event.getY(1));
double radians = Math.atan2(delta_y, delta_x);
if (Constant.TRACE) Log.d("Rotation ~~~~~~~~~~~~~~~~~", delta_x+" ## "+delta_y+" ## "+radians+" ## "
+Math.toDegrees(radians));
return (float) Math.toDegrees(radians);
}
This link is very useful if you are looking a good explanation. By Using this library, you can create a package and copy/past SandboxView, TouchManager and Vector2D classes into that package.
Then, add FrameLayout into your xml file (instead of imageView) and link it to your code.
Finally, add bitmap to the layout by using following code:
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), mUserImgUri);
sandboxView = new SandboxView(mContext, bitmap);
sandboxView.setLayoutParams(new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
sandboxView.setVisibility(View.INVISIBLE);
frameLayout.addView(sandboxView);
} catch (IOException e) {
e.printStackTrace();
}
来源:https://stackoverflow.com/questions/8570900/2-finger-rotation-gesture-listener-in-android