Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

i want to rotate based on center but its rotating based on top left corner .

so i am able to see only 1/4 for rotation of image.

any idea..

Like a old phone dialer .

I have tried like as follows

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

        public boolean onTouchEvent(MotionEvent e) {
                  float x = e.getX();
              float y = e.getY();
              mPreviousX = x;
              mPreviousY = y;

  private void updateRotation(float x, float y) {

          double r = Math.atan2(x - centerX, centerY - y);
            rotation = (int) Math.toDegrees(r);
share|improve this question
First parameter to atan2 must be y in all implementations I know. Besides that, tell us where you are stuck. What is the problem? – Maik Semder Aug 13 '11 at 9:56
i want to rotate based on center but its rotating based on top left corner . this is the issue – Ganapathy Aug 13 '11 at 10:40
up vote 5 down vote accepted

The core problem in your code is probably that you pass in 0, 0 as the pivot point to rotate about in your rotate call.

For a more robust way of expressing your transformation, in linear algebra the way to rotate about an arbitrary point is to translate the world in such a way that the point is at the origin, rotate, and then translate back.

That is, translate(-px, -py) * rotate(theta) * translate(px, py), where p is the point.

share|improve this answer
+1 thanks dude it works.. – Ganapathy Aug 16 '11 at 5:33

You can find an example at installation of android sdk


But maybe you have to change a bit two method to get right orientation on touch event.

private class RotateView extends ViewGroup implements SensorListener {

/* ...  */

    private Matrix invertedMatrix = new Matrix();
    private float[] tempLocation = new float[2];

    protected void dispatchDraw(Canvas canvas) {;
        canvas.rotate(mHeading, getWidth() * 0.5f, getHeight() * 0.5f);
        mCanvas.delegate = canvas;

    public boolean dispatchTouchEvent(MotionEvent ev) {
        centeringAllowed = false;

        float[] location = tempLocation;
        location[0] = ev.getX();
        location[1] = ev.getY();
        ev.setLocation(location[0], location[1]);
        return super.dispatchTouchEvent(ev);

This class is part of MapDemo project

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.