Android 视图和传感器
我有一个使用方向传感器的指南针应用程序,每次传感器更改时,我的指南针视图都会失效,以便向北旋转。我的指南针是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试这个
替换:
sm.registerListener(this,orientation, SensorManager.SENSOR_DELAY_UI);
用这个:
sm.registerListener(this,orientation, SensorManager.SENSOR_DELAY_FASTEST);
如果你想要一些额外的平滑度
替换:
用这个:
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:
with this: