Android – rotates the bitmap at a fixed point like a timer in surfaceview

I have an image in a paintable folder for the clock pointer. I want to rotate it at a fixed point like a timer. I tried the following code. It rotates around a circular path around a fixed point, but not like the clock pointer

 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'm working hard for hours to solve it. Any help will be appreciated

I tried the link below and failed

> SurfaceView, draw image and reduce size, rotate image > Android: How to rotate a bitmap on a center point > How to rotate a bitmap in Android about images center smoothly without oscillatory movement

resolvent:

I found the answer. Let PX and py be any point on the canvas. Bitmap. Getwidth() / 2 is the middle point of the bitmap width. It can be any point of X coordinate. For y coordinate, I take it as 10. Anglerotation is the angle as needed

So matrix. Posttranslate (- bitmap. Getwidth() / 2, - 10); It's a rotation point

Here 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);

The above code meets my requirements. Please modify it according to your needs

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>