Rotate bitmap in fixed point like a clock hand in SurfaceView

核能气质少年 提交于 2019-12-21 05:30:28

问题


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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/35406924/rotate-bitmap-in-fixed-point-like-a-clock-hand-in-surfaceview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!