Android:canvas.drawBitmap性能问题 [英] Android: canvas.drawBitmap performance issue

查看:997
本文介绍了Android:canvas.drawBitmap性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个问题 Android:图像围绕中心的旋转在Unconn的帮助下解决了.

I had an issue lately Android: Rotation of image around the center which was solved by the help of Unconn.

但是,事实证明,与RotateAnimation相比,该解决方案(以及正在运行的)确实具有更差的性能.

However, it turns out, that the solution - as well as it is working - does have a much worse performance, than the RotateAnimation stuff.

到目前为止我所拥有的: 1)具有两个ImageView的FrameLayout,彼此叠置 2)SensorEventHandler,它根据测得的标题向下方移动视图.如果我使用RotateAnimation,这可以接受

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

我想加快速度并稍微平滑动画,但失败了.这是当前的解决方案: 1)具有单独线程的SurfaceView,控制图层的绘制 2)如果不执行下面的代码,该线程可能会达到约50 fps. 3)在下面的代码下,帧速率下降到5ps或更小,因此不再是非常平滑的事情了……

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...

这是代码:

mRadar:显示雷达"的位图,较早地从资源加载 mHeading:从传感器获取的当前航向,以度为单位 mRadarW,mRadarH:图像的初始宽度/高度,在创建时确定

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);

是否知道Matrix/drawBitmap东西执行的不是那么好,实际上比RotateAnimation差? 如果没有机会使用此功能:您可以提出哪些选择?尽管RotateAnimation现在可以使用,但我仍然需要组成一个更加复杂的图像,在之前我必须去RotateAnimation,所以我担心,我会再次与drawBitmap和朋友一起放松……

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...

哦,我想念CALayers:)

Oh, I miss CALayers :)

推荐答案

这一步非常快:

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   

我使用了不错的32位png图像,因此不需要任何滤镜或抗锯齿的Paint ...您对此精灵使用哪种图像?

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?

这篇关于Android:canvas.drawBitmap性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆