Android 视图和传感器

发布于 2025-01-01 22:20:45 字数 1873 浏览 3 评论 0原文

我有一个使用方向传感器的指南针应用程序,每次传感器更改时,我的指南针视图都会失效,以便向北旋转。我的指南针是 444 像素 x 444 像素的位图。我面临的问题是,每当传感器发生变化并且你看到指南针旋转时,它就会滞后,基本上每秒帧数很差。我尝试改用表面视图并更改 SENSOR_DELAY,但没有任何更改。如果有人能帮助我找出这个问题的答案,我会很高兴。

注册传感器

    orientation = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    sm.registerListener(this, orientation, SensorManager.SENSOR_DELAY_UI);

每当传感器发生变化时

public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    if (compass != null) {
        float[] v = event.values;
        north = v[0];
        compass.setNorth(-north);
    }
} 

罗盘视图

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

private void drawBackground(Canvas canvas) {
    canvas.save();
    if (north_changed == true) {
        canvas.rotate(north);
    }
    float maxwidth = (float) (backgroundTexture.getWidth() * Math.sqrt(2));
    float maxheight = (float) (backgroundTexture.getHeight() * Math.sqrt(2));
    float ratio = Math.min(w / maxwidth, h / maxheight);
    int width = (int) (backgroundTexture.getWidth() * ratio);
    int height = (int) (backgroundTexture.getHeight() * ratio);
    canvas.drawBitmap(
            backgroundTexture,
            new Rect(0, 0, backgroundTexture.getWidth(), backgroundTexture
                    .getHeight()), new RectF(-width / 1.5f, -height / 1.5f,
                    width / 1.5f, height / 1.5f), facePaint);
    canvas.restore();
}

onDraw中的方法

protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    if (showCompass) {
        w = getWidth();
        h = getHeight();
        cx = w / 2;
        cy = h / 2;
        canvas.translate(cx, cy);
        loadImages();
        drawBackground(canvas);
    }
}

I have a compass application which is using orientation sensor and every time the sensor changes my compass view is being invalidated in order to rotate towards north. My compass is a 444px x 444px bitmap. The problem I'm facing is that whenever the sensor changes and you see the compass rotating it's lagging, basically poor fps. I've tried to use a surface view instead and to change the SENSOR_DELAY, without any change. Would love if someone could help me figure out the answer to this problem.

Registering sensor

    orientation = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    sm.registerListener(this, orientation, SensorManager.SENSOR_DELAY_UI);

Whenever sensor changes

public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    if (compass != null) {
        float[] v = event.values;
        north = v[0];
        compass.setNorth(-north);
    }
} 

Methods in the compass view

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

private void drawBackground(Canvas canvas) {
    canvas.save();
    if (north_changed == true) {
        canvas.rotate(north);
    }
    float maxwidth = (float) (backgroundTexture.getWidth() * Math.sqrt(2));
    float maxheight = (float) (backgroundTexture.getHeight() * Math.sqrt(2));
    float ratio = Math.min(w / maxwidth, h / maxheight);
    int width = (int) (backgroundTexture.getWidth() * ratio);
    int height = (int) (backgroundTexture.getHeight() * ratio);
    canvas.drawBitmap(
            backgroundTexture,
            new Rect(0, 0, backgroundTexture.getWidth(), backgroundTexture
                    .getHeight()), new RectF(-width / 1.5f, -height / 1.5f,
                    width / 1.5f, height / 1.5f), facePaint);
    canvas.restore();
}

onDraw

protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    if (showCompass) {
        w = getWidth();
        h = getHeight();
        cx = w / 2;
        cy = h / 2;
        canvas.translate(cx, cy);
        loadImages();
        drawBackground(canvas);
    }
}

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

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

发布评论

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

评论(1

勿忘初心 2025-01-08 22:20:45

您可以尝试这个

替换:

sm.registerListener(this,orientation, SensorManager.SENSOR_DELAY_UI);

用这个:

sm.registerListener(this,orientation, SensorManager.SENSOR_DELAY_FASTEST);

如果你想要一些额外的平滑度

替换:

if (compass != null) {
    float[] v = event.values;
    north = v[0];
    compass.setNorth(-north);
}

用这个:

if (compass != null) {
    float[] v = event.values;
    north =north*0.1f + v[0]*0.9f;
    compass.setNorth(-north);
}

You can try this

replace:

sm.registerListener(this, orientation, SensorManager.SENSOR_DELAY_UI);

with this:

sm.registerListener(this, orientation, SensorManager.SENSOR_DELAY_FASTEST);

And if you want some extra smoothness

replace:

if (compass != null) {
    float[] v = event.values;
    north = v[0];
    compass.setNorth(-north);
}

with this:

if (compass != null) {
    float[] v = event.values;
    north =north*0.1f + v[0]*0.9f;
    compass.setNorth(-north);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文