在Horizo​​ntalScrollView上移动ImageView

时间:2013-07-17 14:37:39

标签: java android imageview orientation horizontalscrollview

我正在显示全景照片,我希望我的ImageView根据传感器方向移动(我的手机的方向决定了图像视图,它太宽而无法一次完全显示)。当我调用改变我的ImageView的方法(例如setX)时,我的应用程序崩溃了。我在UI线程中这样做,所以我不知道为什么这不起作用。引用传感器的代码工作正常。

任何帮助都将不胜感激。

public class ImageActivity extends Activity
{
private ImageView imageView;
private LocationManager locationManager;
private String provider;
private SensorManager sensorManager;
public float val = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    sensorManager.registerListener(new CustomOrientationListener(), sensor, 
                            SensorManager.SENSOR_DELAY_GAME);

    //imageView = (ImageView) findViewById(R.id.imageView1);    

}


class CustomOrientationListener implements SensorEventListener {

      public CustomOrientationListener() {}

      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {}

      @Override
      public void onSensorChanged(SensorEvent event) {
          final float[] values;
          if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){
              values = event.values;
              runOnUiThread(new Runnable(){

                @Override
                public void run() {
                    imageView.setTranslationX((int)values[0]);

                }

              });
          }

      }

  }

}

1 个答案:

答案 0 :(得分:-1)

是的,我在我的电脑上安装了Android SDK,但是我在手机上测试了我的应用程序,所以我不知道如何阅读日志,因为我不知道为什么