问题
I had an issue lately Android: Rotation of image around the center which was solved by the help of Unconn.
However, it turns out, that the solution - as well as it is working - does have a much worse performance, than the RotateAnimation stuff.
What I have so far: 1) FrameLayout with two ImageViews, on on top of each other 2) SensorEventHandler, which moves the lower view around according to the heading measured. This works acceptable if I use RotateAnimation
I wanted to speed it up and to smooth the animation a bit, but failed dramatically. Here is the current solution: 1) SurfaceView with separate thread, controlling the drawing of the layers 2) Without performing the code below, the thread could reach around 50 fps. 3) With the code below the frame rate goes down to 5ps and less, so it is no longer something very smooth...
Here is the code:
mRadar: Bitmap showing a "radar", loaded from ressource earlier mHeading: The current heading taken from the sensors in degrees mRadarW,mRadarH: Initial width/height of the image, determined on creation
Matrix m = new Matrix();
m.setRotate(mHeading, mRadarW/2, mRadarH/2);
Bitmap rotated_radar = Bitmap.createBitmap(mRadar, 0, 0, mRadarW, mRadarH, m, true);
canvas.drawBitmap(rotated_radar, (mRadarW - rotated_radar.getWidth())/2, (mRadarH - rotated_radar.getHeight())/2, null);
canvas.drawBitmap(mRadar, 0, 0, null);
Is the Matrix/drawBitmap stuff known to perform not that good, in fact worse than the RotateAnimation? If there is no chance to use this: What options could you propose? Although the RotateAnimation would work for now, I would need to compose a much more complex image, before I would have to go for RotateAnimation, so I fear, I will loose again with drawBitmap and friends...
Oh, I miss CALayers :)
回答1:
This one is very fast:
canvas.save(); //save the position of the canvas
canvas.rotate(angle, X + (ballW / 2), Y + (ballH / 2)); //rotate the canvas
canvas.drawBitmap(ball, X, Y, null); //draw the ball on the rotated canvas
canvas.restore(); //"rotate" the canvas back so that it looks like the ball has rotated
I use a nice 32-bit png image, hence don't need any filter or anti-alias Paint... what kind of image do you use for this sprite?
来源:https://stackoverflow.com/questions/5186212/android-canvas-drawbitmap-performance-issue