Monday, 29 October 2012

Get Device Angle


public class Get_Angle extends Activity  implements SensorEventListener{

public static final String tag = Get_Angle.class.getSimpleName();
public static Integer x;
public static Integer y;
private SensorManager sensorManager = null;
TextView txtanglex,txtangley;
Bitmap oldBitmap ;
String filePath = null;
float degrees;
ImageView img;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

setContentView(R.layout.main);

bindComponent();
addLisners();
}
private void addLisners() {
// TODO Auto-generated method stub

}

private void bindComponent() {
// TODO Auto-generated method stub
txtanglex = (TextView)findViewById(R.id.textxpos);
txtangley = (TextView)findViewById(R.id.textypos);
img = (ImageView)findViewById(R.id.imageView1);
}
private void init() {

// TODO Auto-generated method stub
oldBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arleft);
int w = oldBitmap.getWidth();
int h = oldBitmap.getHeight();

Matrix mtx = new Matrix();
degrees = Integer.parseInt(x.toString());
mtx.postRotate(-x);

oldBitmap = Bitmap.createBitmap(oldBitmap, 0, 0, w, h, mtx, true);
img.setImageBitmap(oldBitmap);

}

@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// 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) Math.pow(event.values[0], 2);
y = (int) Math.pow(event.values[1], 2);

txtanglex.setText(x.toString());
txtangley.setText(y.toString());
Log.i("x is","x is.."+x.toString());

}
init();

   }
}

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...