Android: canvas.drawBitmap performance issue

馋奶兔 提交于 2019-12-11 06:27:26

问题


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

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