Rotating images manually can be a little painful, but here's how I did it.
private void animateRotation(int degrees, float durationOfAnimation){ long startTime = SystemClock.elapsedRealtime(); long currentTime; float elapsedRatio = 0; Bitmap bufferBitmap = carBitmap; Matrix matrix = new Matrix(); while (elapsedRatio < 1){ matrix.setRotate(elapsedRatio * degrees); carBitmap = Bitmap.createBitmap(bufferBitmap, 0, 0, width, height, matrix, true);
This will rotate your carBitmap to any angle you specify at the specified time + time to draw the last frame. However, there is a catch. This rotates your carBitmap without properly adjusting its position on the screen. Depending on how you draw bitmaps, you may end up rotating your carBitmap, while the top left corner of the bitmap remains in place. As the car rotates, the bitmap will be stretched and adjusted in accordance with the new size of the car, filling in the spaces around it with transparent pixels. It is difficult to describe how this will look, so an example rotating a square:

The gray area represents the full size of the bitmap and is filled with transparent pixels. To solve this problem, you need to use trigonometry. This is a little complicated ... if this is a problem for you (I donβt know how you draw your bitmap images on canvas so that it could be wrong), and you cannot solve the solution, even if I know, and I will post how I did it.
(I donβt know if this is the most efficient way to do this, but it works smoothly for me, as long as the bitmap is less than 300 Γ 300 or so. Maybe if someone knows about the best way, they can tell us!)
Steve Haley Mar 13 2018-10-18T00: 00Z
source share