Android:canvas.drawBitmap 性能问题

发布于 2024-10-20 05:01:20 字数 1200 浏览 2 评论 0原文

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

然而,事实证明,该解决方案 - 以及它正在工作 - 的性能确实比 RotateAnimation 的东西差得多。

到目前为止我所拥有的: 1)具有两个 ImageView 的 FrameLayout,彼此重叠 2) SensorEventHandler,根据测量的航向移动下部视图。如果我使用 RotateAnimation,这可以接受

,我想加快速度并稍微平滑动画,但失败了。这是当前的解决方案: 1)SurfaceView具有单独的线程,控制图层的绘制 2) 如果不执行下面的代码,线程的速度可能会达到 50 fps 左右。 3)使用下面的代码,帧速率下降到 5ps 或更少,所以它不再是非常平滑的东西......

这是代码:

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

        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 和朋友们失去联系......

哦,我想念 CALayers :)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2024-10-27 05:01:20

这个速度非常快:

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 图像,因此不需要任何滤镜或抗锯齿画图...你为这个精灵使用什么样的图像?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文