Rotating Image on A canvas in android

前端 未结 6 1172
心在旅途
心在旅途 2020-11-27 13:33

I want to Rotate Image according to a specific angle in android ,some thing like a compass...

I have this code...it works on drawPath() but i want to replace the pat

6条回答
  •  情书的邮戳
    2020-11-27 14:17

    Use following code. it worked for me

    float rotation = 30.0f;

         Bitmap bitmap = your bitmap
         Rect rect = new Rect(100,100,bitmap.width, bitmap.height);
         Matrix matrix = new Matrix();
         float px = rect.exactCenterX();
         float py = rect.exactCenterY();
         matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2);
         matrix.postRotate(rotation);
         matrix.postTranslate(px, py);
         canvas.drawBitmap(bitmap, matrix, null);
         matrix.reset();
         invalidate();
    

提交回复
热议问题