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

}
4

1 回答 1

0

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

于 2012-01-25T20:36:48.210 回答