在 Android 中使用大尺寸可绘制视图和自定义视图

发布于 2024-12-16 16:02:49 字数 904 浏览 2 评论 0原文

我知道这已经被反复讨论过,但我找不到适合我需求的解决方案。

场景:为了让说明简单,我有一个自定义视图,它显示两个图像,一个大图像作为背景,一个较小的图像作为轮子。用户可以使用 onTouch 事件旋转/缩放背景图像。他还可以旋转轮子图像来进行一些操作。一切都是在自定义视图而不是 SurfaceView 上完成的,因为我需要它是透明的。

问题:使用onDraw(),我总是需要检查什么旋转/缩放有背景图像,缩放/旋转它,然后绘制它。如果背景图像较小,例如 512x512,则滚轮图像旋转就可以了。如果背景图像较大(1280x1707),则滚轮图像旋转会滞后。所以我的猜测是,对于每个 onDraw() 来说,在背景中操作和旋转大图像都会给我带来性能问题,因为基本上背景图像应该仅在用户操作它时才重绘。

旋转是通过以下方式完成的:

    canvas.save();
    float dx = (maxX + minX) / 2;
    float dy = (maxY + minY) / 2;
    drawable.setBounds((int) minX, (int) minY, (int) maxX, (int) maxY);
    canvas.translate(dx, dy);
    canvas.rotate(angle * 180.0f / (float) Math.PI);
    canvas.translate(-dx, -dy);
    drawable.draw(canvas);
    canvas.restore();

可能的解决方案:我可以创建一个新的自定义视图,它只能绘制背景图像,并在其顶部放置我当前的视图并发送触摸事件,当情况发生时到我的背景图像视图。这将允许我仅在需要时重新绘制背景图像。还有其他想法吗?

I know this has been discussed over and over again but I can't find a solution to suit my needs.

Scenario: To keep the explanation simple, I have a custom view which displays two images a big one for the background and a smaller one as a Wheel. The user can rotate/scale the background image with onTouch events. He also can rotate the Wheel image to make some operations. Everything is done on a custom View not SurfaceView because I need it to be transparent.

The problem: With onDraw() I always need to check what Rotation/Scale has the background image, scale/rotate it and then draw it. If the background image is smaller, let's say 512x512 the Wheel image rotation is fine. If the background image is bigger, 1280x1707, the wheel image rotation is laggy. So my guess is, manipulation and rotation of a big image in background, for each onDraw() gives me performance issues, when basically the background image should be redrawn only the the user manipulates it.

The rotation is done in something like:

    canvas.save();
    float dx = (maxX + minX) / 2;
    float dy = (maxY + minY) / 2;
    drawable.setBounds((int) minX, (int) minY, (int) maxX, (int) maxY);
    canvas.translate(dx, dy);
    canvas.rotate(angle * 180.0f / (float) Math.PI);
    canvas.translate(-dx, -dy);
    drawable.draw(canvas);
    canvas.restore();

Possible solutions: I could make a new custom View which could only draw the background image and on top of it, put my current View and send touch events, when the case to my background image view. This will allow me to redraw the background image only when needed. Any other ideas ?

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

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

发布评论

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

评论(1

风吹雪碎 2024-12-23 16:02:50

我遇到了类似的性能问题,我找到的解决方案就是我在此处描述的。与您已经建议的类似,基本上背景仅绘制一次,自定义 ImageView 处理其触摸事件的所有转换。

I was having similar performance problems and the solution I found is what I described here. Similar to what you already suggested, basically the background is only drawn once and the custom ImageView handles all transformations on its touch events.

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