2014-03-25 216 views
0

我從ImageView創建了一個視圖。此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?

我想支持更多的設備。

回答

1

你已經知道了位置,這是你的x和y。

你需要做的是停止屏幕下降這些值。

首先,讓你的屏幕大小:

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

現在大小保持屏幕尺寸。

然後將以下添加到您的方法

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完美地適用於此。

+2

不要忘記考慮物體的大小,寬度和高度應該除以2.否則它仍然會部分落在屏幕之外。 –

1

可以通過調用getTop()getLeft()功能,這返回x和y分別座標,參考檢索視圖的位置:http://developer.android.com/reference/android/view/View.html#Position

選擇的類型取決於目標offcourse,使用Acceleromtere裝置測量的加速度該裝置在重力下測量裝置上的重力。 請注意加速度數值包括這裏提到的重力數值:http://developer.android.com/guide/topics/sensors/sensors_motion.html

我個人建議使用加速度計或陀螺儀。陀螺儀不使用加速度,而是設備在空間中的實際方向,這可能是您實際尋找的。