Android视图和传感器

Android视图和传感器,android,view,sensors,Android,View,Sensors,我有一个使用方位传感器的指南针应用程序,每次传感器改变时,我的指南针视图都会失效,以便向北旋转。我的指南针是444px4444px位图。我面临的问题是,每当传感器发生变化,你看到指南针旋转时,它就会滞后,基本上fps很差。我试着用表面视图来代替,并且在没有任何改变的情况下改变传感器的延迟。如果有人能帮我找出这个问题的答案,我会很高兴的 记录传感器 orientation = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION); sm.regi

我有一个使用方位传感器的指南针应用程序,每次传感器改变时,我的指南针视图都会失效,以便向北旋转。我的指南针是444px4444px位图。我面临的问题是,每当传感器发生变化,你看到指南针旋转时,它就会滞后,基本上fps很差。我试着用表面视图来代替,并且在没有任何改变的情况下改变传感器的延迟。如果有人能帮我找出这个问题的答案,我会很高兴的

记录传感器

    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);
    }
} 
compass视图中的方法

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();
}
昂德劳

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);
    }
}
你可以试试这个

替换:

if (compass != null) {
    float[] v = event.values;
    north =north*0.1f + v[0]*0.9f;
    compass.setNorth(-north);
}
sm.registerListener(此、方向、传感器管理器、传感器延迟用户界面)

使用此选项:

if (compass != null) {
    float[] v = event.values;
    north =north*0.1f + v[0]*0.9f;
    compass.setNorth(-north);
}
sm.registerListener(此、方向、传感器管理器、传感器延迟)


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

替换:

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

loadImages();你是否在每个画框中都加载图像?愚蠢的我,在构造器中加载图像修复了这个问题。谢谢