I am displaying a panoramic photo, and I want my ImageView to move according to the Sensor orientation (my phone's orientation determines the image view and it's too wide to display it entirely at once). When I invoke methods which change my ImageView (e.g. setX) my application crashes. I am doing it in the UI thread, so I don't know why this doesn't work. Code which refers to the sensor works fine.
Any help would be appreciated.
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]);
}
});
}
}
}
}