dhams dhams - 7 months ago 92
Android Question

How to rotate image with fix point along with touch in android

I've been looking for over a day for a solution to this problem but nothing helped, even the answers here didn't helped me.
my problem is my image is rotating but bitmap size is not varying while rotating.

here is my code

package dharam.rotate;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class Rotate extends Activity {
View baseView ;
ImageView turntable;
TextView bottomText;
GestureDetector gestureDetector;
private float x , y;

public void onCreate(Bundle savedInstanceState) {

baseView = (View) findViewById(R.id.baseView);
turntable = (ImageView) findViewById(R.id.turntable);


public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent evt) {
double r = Math.atan2(evt.getX() - turntable.getWidth() / 2,
(turntable.getHeight() / 2) - evt.getY());
int rotation = (int) Math.toDegrees(r);
Log.i("R is ", ""+r);
if (evt.getAction() == MotionEvent.ACTION_DOWN) {

if (evt.getAction() == MotionEvent.ACTION_MOVE) {
x= evt.getX();
y= evt.getY();

if (evt.getAction() == MotionEvent.ACTION_UP) {
return true;
private void updateRotation(double rot) {
float newRot = new Float(rot);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
Matrix matrix = new Matrix();
// matrix.setTranslate(getWindowManager().getDefaultDisplay().getWidth()/2, getWindowManager().getDefaultDisplay().getHeight());
// matrix.setSinCos(newRot, newRot/2, 100, 100);
// matrix.postRotate(newRot);
Log.i("THE ROATTION ", " "+ newRot);

Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap
.getWidth(), bitmap.getHeight(), matrix, true);
Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap
.getWidth(), bitmap.getHeight(), matrix, true);
Log.i("GUITAR _IMAGE", "");




I presume you want to rotate an image at the point where a user touches the screen? If so, extend the SimpleOnGestureListener like this example:

public class MyGestureDetector extends SimpleOnGestureListener

    public void onLongPress(MotionEvent event)
        int X = (int)event.getX();          
        int Y = (int)event.getY();

        ...Rotate the image

Once you've got the screen coordinates of the touch event, you could apply a Rotation Animation about the point - see here for more details: http://developer.android.com/guide/topics/graphics/2d-graphics.html