机器人:从加速度计值的运动外形/图像 [英] android: moving shape/image with values from accelerometer

查看:187
本文介绍了机器人:从加速度计值的运动外形/图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

香港专业教育学院设法摆脱加速度值(X,Y,Z)。有一个简单的方法,使这些价值观圈的举动?我也希望它停在屏幕的边缘。谢谢!

ive managed to get out the accelerometers values (x,y,z). Is there an easy way to make a circle move with those values? I would also like it to stop at the edges of the screen. Thanks!

推荐答案

我觉得你可以做这样的事情(注意:部分伪code):

I think you can do something like this (note: partially pseudocode):

public void onSensorChanged (int sensor, float[] values) {
    //adjust someNumber to desired speed
    //values[1] can be -180 to 180
    float xChange = someNumber * values[1];
    //values[2] can be -90 to 90
    float yChange = someNumber * 2 * values[2];

    //only move object if it will stay within the bounds
    if (object.xPos + xChange > 0 && object.xPos + xChange < xBorder) {
        object.xPos += xChange;
    }
    if (object.yPos + yChange > 0 && object.yPos + yChange < yBorder) {
        object.yPos += yChange;
    }
    //force a repaint of your surface here
}

其中:


  • onSensorChanged 是一个每次加速动作......我不知道你是否使用的是调用的方法的的SensorManager ,但似乎方便了您的方案。请注意,您必须自己实现这个方法。

  • 对象是要移动的圈子。

  • xBorder yBorder 是对象的运动的最大边界。最小边界被假定为0和0,虽然你可以使用任何你喜欢的。

  • onSensorChanged is a method that is called each time the accelerometer moves... I'm not sure whether you are using SensorManager, but it seems convenient for your scenario. Note that you must implement this method yourself.
  • object is the circle you want to move.
  • xBorder and yBorder are the maximum bounds for the object's movement. The minimum bounds are assumed to be 0 and 0, though you can use whatever you like.

这篇关于机器人:从加速度计值的运动外形/图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆