安卓图形

发布于 2024-12-29 08:57:00 字数 1235 浏览 1 评论 0原文

我有一个指南针,它是视图类的子类,我的指南针指针是位图。每次传感器发生变化,罗盘就会失效并在画布上重新绘制,罗盘针的旋转也会发生变化。我遇到的问题是,当你看到指南针旋转时,它有点滞后,基本上帧率很差。我猜它是滞后的,因为我不断地重新绘制位图而不是实际绘制,并且想知道如果我使用表面视图而不是在画布上绘制,糟糕的 fps 是否会消失。任何有关如何以最佳方式解决此问题的提示将不胜感激。图像尺寸为 300x300 像素。

主要活动中的传感器:

@Override
public void onSensorChanged(SensorEvent event) {
    float[] v = event.values;
    north = v[0];
    comp.setNorth(-v[0]);
}

罗盘视图:

public void setNorth(float n) {
    north_changed = true;
    north = n;
    invalidate();
}

绘制位图:

private void drawCompass(Canvas canvas) {
    canvas.save();
    if (north_changed == true) {
        canvas.rotate(north);
    }
    float maxwidth = (float) (canvasBitmap.getWidth() * Math.sqrt(2));
    float maxheight = (float) (canvasBitmap.getHeight() * Math.sqrt(2));
    float ratio = Math.min(w / maxwidth, h / maxheight);
    int width = (int) (canvasBitmap.getWidth() * ratio);
    int height = (int) (canvasBitmap.getHeight() * ratio);

    canvas.drawBitmap(compTexture, new Rect(0, 0, compTexture.getWidth(),
            compTexture.getHeight()), new Rect(-width / 2, -height / 2,
            width / 2, height / 2), facePaint);
    canvas.restore();

}

I have a compass that's a subclass to the view class and my compass needle is a bitmap. Everytime the sensor changes the compass is invalidated and redrawn on the canvas, the rotation of the compass needle also changes. The problem i'm having is that when you see the compass needle rotating it's lagging a bit, basically poor fps. I'm guessing it's lagging because I'm redrawing a bitmap constantly instead of actually drawing, and wondering if the poor fps would go away if I used a surface view instead to draw on the canvas. Any tips on how to solve this the best way would be greatly appreciated. The image is 300x300 px.

Sensor in main activity:

@Override
public void onSensorChanged(SensorEvent event) {
    float[] v = event.values;
    north = v[0];
    comp.setNorth(-v[0]);
}

Compass view:

public void setNorth(float n) {
    north_changed = true;
    north = n;
    invalidate();
}

Drawing bitmap:

private void drawCompass(Canvas canvas) {
    canvas.save();
    if (north_changed == true) {
        canvas.rotate(north);
    }
    float maxwidth = (float) (canvasBitmap.getWidth() * Math.sqrt(2));
    float maxheight = (float) (canvasBitmap.getHeight() * Math.sqrt(2));
    float ratio = Math.min(w / maxwidth, h / maxheight);
    int width = (int) (canvasBitmap.getWidth() * ratio);
    int height = (int) (canvasBitmap.getHeight() * ratio);

    canvas.drawBitmap(compTexture, new Rect(0, 0, compTexture.getWidth(),
            compTexture.getHeight()), new Rect(-width / 2, -height / 2,
            width / 2, height / 2), facePaint);
    canvas.restore();

}

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

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

发布评论

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

评论(1

﹉夏雨初晴づ 2025-01-05 08:57:00

使用 SurfaceView 更适合此任务,它每秒可以渲染更多帧,您还可以使用 GLSurfaceView 来确保所有内容都在 GPU 上渲染。

Using SurfaceView suits this task much better, it can render many more frames per second, you can also use GLSurfaceView to make sure everything is being rendered on the GPU.

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