4

I have an image in drawable folder for clock hand. I want to rotate it in fixed point like a clock hand. I have tried below code which rotates the hand in circular path around a fixed point but not as like clock hand.

 Matrix matrix = new Matrix();
 matrix.reset();
 matrix.preTranslate(0, 0);
 matrix.postRotate(angleRotation, -0, -0);
 matrix.postTranslate(240, 480);

 canvas.drawColor(Color.WHITE);
 canvas.drawBitmap(bitmap, matrix, null);

I am struggling for hours to get it sorted out. Any help will be greatly appreciated.

I have tried below links for help as well with no success.

  1. SurfaceView, draw image and reduce size, rotate image

  2. Android: How to rotate a bitmap on a center point

  3. How to rotate a bitmap in Android about images center smoothly without oscillatory movement

Community
  • 1
  • 1
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53

2 Answers2

3

I found the answer. Let px and py be any point on canvas. bitmap.getWidth()/2 is middle point along bitmap width. It can be any point for x cordinate. For y cordinate I have taken it as 10. angleRotation is the angle that is as per required.

So matrix.postTranslate(-bitmap.getWidth()/2, -10); is the point of rotation.

Below is the code.

        Matrix matrix = new Matrix();
        matrix.reset();
        Paint paint = new Paint();
        float px = 240;
        float py = 480;
        matrix.postTranslate(-bitmap.getWidth()/2, -10);
        matrix.postRotate(angleRotation);
        matrix.postTranslate(px, py);
        canvas.drawBitmap(bitmap, matrix, paint);

Above code satisfy my requirements. Please modify it as per your need.

Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53
0

I use this to rotate my bitmaps.

 private Bitmap rotateImage(Bitmap src,int degrees) {
         Matrix matrix = new Matrix();
         matrix.postRotate(degrees);
        return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}
Nick Isaacs
  • 172
  • 11