Sarmad parvaiz
Sarmad parvaiz

Reputation: 61

Rotate Bitmap on Canvas Android

I have a class extending SurfaceView. I have an image of a needle which i have drawn on canvas through Bitmap. I want to rotate this needle with one fixed point on click of a button. What is the logic for rotating needle ? Suppose i have called onDraw(Canvas canvas) on click of a button and i want to rotate needle by 5 degrees every time. this is my code

    public class PingPongSurfaceView extends SurfaceView implements SurfaceHolder.Callback
    {
      private Bitmap needle = null;

      public PingPongSurfaceView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
            setBackgroundResource(R.drawable.puzzle);

        needle = BitmapFactory.decodeResource(getResources(), R.drawable.sui);


        }

protected void onDraw(Canvas canvas) 
          {
        int needlex =0;
        int needley = 0;

            needlex = (mWidth/2) - needle.getWidth()+9;
    needley = (mHeight - (needle.getHeight()/2)-70);

            canvas.drawBitmap(needle, needlex, needley, null); //Bitmap to be rotate

          }

    }

Upvotes: 0

Views: 3123

Answers (1)

Yoric
Yoric

Reputation: 508

You should call canvas.save() then canvas.rotate(5, centerX, centerY) canvas.drawBitmap(...) and then canvas.restore()

Upvotes: 7

Related Questions