图像在android中没有旋转

时间:2014-03-19 05:11:10

标签: android image rotation

我在不同的framelayouts中有2张图像,两张图像都附有触控侦听器。 两者都在一个屏幕上。第一张图像旋转成功但第二张图像没有旋转。 我正在尝试使用touchlistener旋转,缩放和移动图像。

这是我的代码:

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crop);


    image1 = (ImageView) findViewById(R.id.image1);
    image1.setOnTouchListener(this);

    image2 = (ImageView) findViewById(R.id.image2);
    image2.setOnTouchListener(this);

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
    image1.setImageBitmap(bitmap);
    mImageHeight = bitmap.getHeight();
    mImageWidth = bitmap.getWidth();

    mMatrix.postScale(mScaleFactor, mScaleFactor);
    image1.setImageMatrix(mMatrix);
    image2.setImageMatrix(mMatrix);

    mScaleDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleListener());
    mRotateDetector = new RotateGestureDetector(getApplicationContext(), new RotateListener());
    mMoveDetector = new MoveGestureDetector(getApplicationContext(), new MoveListener());

}


public boolean onTouch(View v, MotionEvent event) {
     mScaleDetector.onTouchEvent(event); 
    mRotateDetector.onTouchEvent(event);
    mMoveDetector.onTouchEvent(event);

    float scaledImageCenterX = (mImageWidth * mScaleFactor) / 2;
    float scaledImageCenterY = (mImageHeight * mScaleFactor) / 2;

    mMatrix.reset();
    mMatrix.postScale(mScaleFactor, mScaleFactor); // 12-march
    mMatrix.postRotate(mRotationDegrees, scaledImageCenterX, scaledImageCenterY);
    mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);// 12-march

    ImageView view = (ImageView) v;
    view.setImageMatrix(mMatrix);
    return true;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));

        return true;
    }
}

private class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
    @Override
    public boolean onRotate(RotateGestureDetector detector) {
        mRotationDegrees -= detector.getRotationDegreesDelta();
        return true;
    }
}

private class MoveListener extends MoveGestureDetector.SimpleOnMoveGestureListener {
    @Override
    public boolean onMove(MoveGestureDetector detector) {
        PointF d = detector.getFocusDelta();
        mFocusX += d.x;
        mFocusY += d.y;
        return true;
    }
}

0 个答案:

没有答案