1

I want to rotate the canvas circularly on its center axis based on user touch.

Like a old phone dialer .

i want to rotate based on center

but

Now its rotating based on top left corner .so i am able to see only 1/4 for rotation of image.

I have tried like as follows

onDraw(Canvas canvas){
  canvas.save();
  // do my rotation
  canvas.rotate(rotation,0,0);
  canvas.drawBitmap( ((BitmapDrawable)d).getBitmap(),0,0,p );
  canvas.restore();
}

@Override
        public boolean onTouchEvent(MotionEvent e) {
                  float x = e.getX();
              float y = e.getY();
              updateRotation(x,y);
              mPreviousX = x;
              mPreviousY = y;
            invalidate();
        }

  private void updateRotation(float x, float y) {

          double r = Math.atan2(x - centerX, centerY - y);
            rotation = (int) Math.toDegrees(r);
        }
Kara
  • 5,996
  • 16
  • 49
  • 56
Ganapathy C
  • 5,889
  • 5
  • 41
  • 73

3 Answers3

2

Instead of rotating the Canvas you can use x=0;

x=x+50;
yourview.setRotation(x);

use this on touch event and x=x-50 for rotating backword

I think it will help

Bora
  • 1,913
  • 3
  • 27
  • 53
2

You need to add this to your custom view methods

@Override     
public void onSizeChanged (int w, int h, int oldw, int oldh){ 
  super.onSizeChanged(w, h, oldw, oldh);         

  screenW = w;         
  screenH = h; 
}

This method will give you the canvas size then use

canvas.rotate(rotation, screenW / 2, screenH / 2);

Lumis
  • 21,237
  • 8
  • 61
  • 67
  • +1 for answer. Thanks dude this onSizeChanged method i left previously. now its working great. – Ganapathy C Aug 13 '11 at 13:16
  • 1
    great, if you need to rotate an image which is not in the center of the canvas, then just enter image's center as pivot rotate coordinates – Lumis Aug 13 '11 at 13:23
2

Pass the point of rotation to rotate api:

canvas.rotate(rotation, 0, centerY);
hasanghaforian
  • 13,498
  • 9
  • 73
  • 157
Ronnie
  • 24,023
  • 8
  • 54
  • 95
  • thanks +1 for answer but canvas.rotate(rotation,0,centerY) gives me only half side (right half rotation) ,But canvas.rotate(rotation,centerX,centerY) gives me exact circular rotation on its midpoint. – Ganapathy C Aug 13 '11 at 13:19