Android:麻烦在方向传感器Y轴(音高)上旋转位图

时间:2012-06-18 08:55:51

标签: android bitmap draw android-sensors rotateanimation

你好,我迫切需要帮助,我有一个转向的图像,我想在手机向左或向右倾斜时旋转它。我的应用程序处于横向模式。你应该看到下面的代码。问题是位图不会旋转并在不同位置重绘。我无法找到问题。

public class Accelerometer extends Activity {

 mySteering steering = null;
 private static SensorManager mySensorManager;
 private boolean sensorrunning;
 public  float y;

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

       steering = new mySteering(this);

       setContentView(steering);

       mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
       List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);

       if(mySensors.size() > 0){
        mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
        sensorrunning = true;
        Toast.makeText(this, "Start ORIENTATION Sensor", Toast.LENGTH_LONG).show();
       }
       else{
        Toast.makeText(this, "No ORIENTATION Sensor", Toast.LENGTH_LONG).show();
        sensorrunning = false;
        finish();
       }

   }

   private SensorEventListener mySensorEventListener = new SensorEventListener() {

  @Override
  public void onSensorChanged(SensorEvent event) {
   // TODO Auto-generated method stub


   y = event.values[1];
  }

  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
   // TODO Auto-generated method stub

  }
 };

 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();

  if(sensorrunning){
   mySensorManager.unregisterListener(mySensorEventListener);
   Toast.makeText(Accelerometer.this, "unregisterListener", Toast.LENGTH_SHORT).show();
  }
 }

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
}

public class mySteering extends View {
    static final int width = 50;
    static final int height = 50;

    Accelerometer sensor = new Accelerometer();
    Matrix rot = new Matrix();

    public mySteering(Context context) {
        super(context);

    }

    protected void onDraw(Canvas canvas) {

        Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.steering);
        Paint p = new Paint(); // set some paint options
        p.setColor(Color.WHITE);

        int canvasWidth = canvas.getWidth();
        int canvasHeight = canvas.getHeight();
        int centreX = (canvasWidth) / 2;

        int centreY = (canvasHeight) / 2;

        canvas.drawText("This is on Y   " + sensor.y, 5, 40, p);



        if (sensor.y <= 12.0 && sensor.y >= -12.0) {


             canvas.drawBitmap(bmp, centreX, centreY, null);
             invalidate();
             }  

        if (sensor.y > 20.0 && sensor.y < 0.0) {

         rot.setRotate(45, centreX, centreY);
         canvas.drawBitmap(bmp, rot, p);
         invalidate();
         } 



         if (sensor.y < -20.0 && sensor.y > 0.0 ) {
             rot.setRotate(-45, centreX, centreY);
             canvas.drawBitmap(bmp, rot, p);
             invalidate();
         }

有人可以弄清楚或改变代码中的错误吗?提前致谢。 这就是它的外观如何不旋转甚至不重绘。

screenshot

1 个答案:

答案 0 :(得分:0)

您可能必须按如下方式传递传感器值:

 public void onSensorChanged(SensorEvent event) {
   // TODO Auto-generated method stub

   steering.sensor = this;
   y = event.values[1];
   steering.invalidate();
  }