0
我想旋轉我的位圖img這是一個使用加速度計的一個軸的汽車轉向。我不知道如何在矩陣中使用它或在其他類中調用sensorchanged方法。我的應用程序處於橫向模式,我有兩個類用於傳感器和一個用於位圖。我完全失去了。Android:如何使用加速度計旋轉我的位圖?
public class CustomDrawableView extends View {
AnimationSteeringActivity sens = new AnimationSteeringActivity();
public CustomDrawableView(Context context) {
// TODO Auto-generated constructor stub
super(context);
}
@Override
public void onDraw(Canvas canvas) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.steering);
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
int bitmapWidth = bmp.getWidth();
int bitmapHeight = bmp.getHeight();
int centreX = (canvasWidth - bitmapWidth)/2;
int centreY = (canvasWidth - bitmapHeight)/2;
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bmp, centreX, centreY, null);
和
public class AnimationSteeringActivity extends Activity implements
SensorEventListener {
/** Called when the activity is first created. */
/** Called when the activity is first created. */
CustomDrawableView mCustomDrawableView = null;
ShapeDrawable mDrawable = new ShapeDrawable();
public static int x;
public static int y;
private SensorManager sensorManager = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get a reference to a SensorManager
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mCustomDrawableView = new CustomDrawableView(this);
setContentView(mCustomDrawableView);
// setContentView(R.layout.main);
}
// This method will update the UI on new sensor events
public void onSensorChanged(SensorEvent sensorEvent) {
{
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// the values you were calculating originally here were over
// 10000!
x = (int) Math.pow(sensorEvent.values[1], 2);
y = (int) Math.pow(sensorEvent.values[2], 2);
}
if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
}
}
}
我知道如何做到這一點與矩陣,但我不知道如何通過x和y從傳感器類 – user1422066
從onsensorchanged得到正確的值,並把它放在matrix.postRotate(-90); //逆時針方向90 90 – QVDev
如何將傳感器值與其他類中的矩陣放在一起我無法弄清楚請看我的代碼。 – user1422066