- Demand
In SurfaceView or ordinary view, in each drawing cycle, we not only need to update the drawing of the Bitmap object in the View, but also hope that the Bitmap can use its own center point as the center of the center to rotate. - Solution
Use Canvas’s DrawbitMap (bitmap bitmap, matrix matrix, paint paint) method. The most important thing is custom Matrix.
code is as follows:
/**
* Draw a spin to transform
*
* @param canvas
* @param paint
* @parambitmap
* Bit chart object
* @paramrotation
* Rotating degree
* @paramPOSX
* Position coordinates at Canvas
* @param posY
*/
private void drawRotateBitmap(Canvas canvas, Paint paint, Bitmap bitmap,
float rotation, float posX, float posY) {
Matrix matrix = new Matrix();
int offsetX = bitmap.getWidth() / 2;
int offsetY = bitmap.getHeight() / 2;
matrix.postTranslate(-offsetX, -offsetY);
matrix.postRotate(rotation);
matrix.postTranslate(posX + offsetX, posY + offsetY);
canvas.drawBitmap(bitmap, matrix, paint);
}
First, we move the bitmap half (half of XY) to the upper left corner, and then rotate the required degree. Finally move the Center back. Then move to the position coordinate (POSX, POSY). Note that the coordinates (POSX, POSY) are the points in the upper left corner of the map.
In addition, in order to make the rotation coherent, when the method is called:
rotation += 0.1f * (new Random().nextInt(20));
drawRotateBitmap(canvas, paint, bitmap, rotation, posX, posY);
- More communication
Android Development Alliance QQ Group: 272209595