【问题标题】:Android: How to rotate my Bitmap using accelerometer?Android:如何使用加速度计旋转我的位图?
【发布时间】:2012-06-02 22:12:29
【问题描述】:

我想旋转我的位图 img,它是使用一个加速度计轴的汽车转向。我不知道如何将它与矩阵一起使用或在其他类中调用 sensorchanged 方法。我的应用程序处于横向模式,我有两个类,一个用于传感器,一个用于位图。我完全迷路了。

    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) {

        }
    }
}

【问题讨论】:

    标签: android bitmap accelerometer image-rotation


    【解决方案1】:

    在此链接中寻找实际旋转

    rotating a bitmap

    所以在 onSensorChanged 方法中,您将根据需要使用所需的 x 或 y 值进行旋转

    【讨论】:

    • 我知道如何使用矩阵来做到这一点,但我不知道如何从传感器类中传递 x 和 y
    • 从onsensorchanged中获取正确的值并放入matrix.postRotate(-90); // 逆时针方向 90 表示 90
    • 如何将传感器值放在其他类中,矩阵我不知道请看我的代码。
    • 使用公共 getter 和 setter 在自定义可绘制变量中创建一个变量,然后替换上面提到的旋转值。然后在你的 sensorchanged 中调用 mdrawable.setRotation(value);然后强制更新视图。
    • 感谢完成,但坐标系有问题
    【解决方案2】:
    Bitmap myBitmap = BitmapFactory.decodeStream(downloadImageFromWeb());
    
    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android1);
    
    Display d = getWindowManager().getDefaultDisplay();
    
    int x = d.getWidth();
    
    int y = d.getHeight();
    
    ImageView img1 = (ImageView)findViewById(R.id.img1);
    
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, y, x, true);
    
    Matrix matrix = new Matrix();
    
    matrix.postRotate(-90);
    
    Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
    
    
    img1.setImageBitmap(rotatedBitmap);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多