获取当前位置的ImageView

时间:2014-03-25 15:15:44

标签: android

我从ImageView创建了一个视图。此imageview是屏幕上的球(取决于加速度传感器)。现在我怎样才能在屏幕上获得当前的位置?因为我不希望球出现在屏幕外面。喜欢:

enter image description here

我的所有代码:

public class MainActivity extends Activity implements SensorEventListener {
    private SensorManager sensorManager;
    private Sensor accelerometer;

    AnimatedView animatedView = null;
    ShapeDrawable mDrawable = new ShapeDrawable();
    public static int x;
    public static int y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        accelerometer = sensorManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        animatedView = new AnimatedView(this);
        setContentView(animatedView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this, accelerometer,
                SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub


        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            x -= ((int) event.values[0])*4;
            y += ((int) event.values[1])*4;

        }
    }

    public class AnimatedView extends ImageView {

        static final int width = 100;
        static final int height = 100;

        public AnimatedView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub

            mDrawable = new ShapeDrawable(new OvalShape());
            mDrawable.getPaint().setColor(0xffffAC23);
            mDrawable.setBounds(x, y, x + width, y + height);


        }

        @Override
        protected void onDraw(Canvas canvas) {

            mDrawable.setBounds(x, y, x + width, y + height);
            mDrawable.draw(canvas);
            invalidate();
        }
    }

}

其他问题, 哪一个更好用?TYPE_ACCELEROMETER或TYPE_GRAVITY?

我想支持更多设备。

2 个答案:

答案 0 :(得分:1)

您可以通过调用分别返回x和y坐标的getTop()getLeft()函数来检索视图的位置,参考:http://developer.android.com/reference/android/view/View.html#Position

选择类型取决于目标场景,使用Acceleromtere意味着测量设备的加速度,同时重力测量设备上的重力。 请注意,加速器值包括此处提到的重力值:http://developer.android.com/guide/topics/sensors/sensors_motion.html

我个人建议使用加速度计或陀螺仪。陀螺仪不使用加速度,而是使用设备在空间中的实际方向,这可能是您实际需要的。

答案 1 :(得分:1)

您已经知道了位置,即您的x和y。

您需要做的是阻止这些值掉线。

首先,获取屏幕大小:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);

现在size持有screenSize。

然后将以下内容添加到您的方法

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

           x -= ((int) event.values[0])*4;
           y += ((int) event.values[1])*4;
           if(x < 0)
               x = 0;
           else if(x > size.x)
               x = size.x
           if(y < 0)
               y = 0;
           else if (y > size.y)
               y = size.y

    }

我还建议您使用线程来处理x和y位置。您可以控制线程运行的频率,传感器将在每次事件发生时触发。

为了更好地了解Android传感器,请阅读以下内容:http://www.codeproject.com/Articles/729759/Android-Sensor-Fusion-Tutorial 一些传感器是硬件传感器,另一些是组合硬件传感器的软件传感器。

在你的情况下:我不确定你的应用程序的用途是什么,但我想你想通过倾斜屏幕来移动球。我会说GRAVITY非常适用于此。

相关问题