旋转图像onclick问题

时间:2012-06-13 23:05:57

标签: android image onclick rotation

我还在处理一段代码,我想使用按钮顺时针和逆时针旋转图像,我尝试了以下代码:

case (R.id.clock):
            matrix.postRotate(90);
            matrix.postRotate(getDegreesFromRadians(angle), mid.x, mid.y);

        break;

        case (R.id.anticlock):
            float degrees = 0;
            degrees = degrees-10;
            matrix.postRotate(degrees); 
            break;

它工作但不是正确的方式,首先我按下按钮然后点击imageview旋转图像。有什么帮助吗?

我也在这里问了这个问题Android Rotate image ontouch 但现在我想用按钮

2 个答案:

答案 0 :(得分:2)

这就是我通常做的事情。我用这种方法创建静态Util类:

public static Bitmap rotate(Bitmap bitmap, int degree) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    Matrix mtx = new Matrix();
    mtx.postRotate(degree);

  return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}

然后,我像这样使用它:

case 21: // ROTATE LEFT
    ImageProcessActivity.processedBitmap = ImageUtil.rotate(ImageProcessActivity.processedBitmap, -90);
    d = new BitmapDrawable(ImageProcessActivity.processedBitmap);
    ImageProcessActivity.selectedImage.setImageDrawable(d);

break;
case 22: // ROTATE RIGHT
    ImageProcessActivity.processedBitmap = ImageUtil.rotate(ImageProcessActivity.processedBitmap, 90);
    d = new BitmapDrawable(ImageProcessActivity.processedBitmap);
    ImageProcessActivity.selectedImage.setImageDrawable(d);
break;

您会看到,在我的代码中,我将使用ImageView显示的图像分开。这样,我可以轻松地操作图像,而无需使用ImageView。顾名思义,ImageView仅用于查看图像。不是操纵的来源。

答案 1 :(得分:0)

您必须通知您的视图已更改,以便android将其渲染为egain。视图不是frameRate渲染的,所以在更改矩阵之后这样做:

yourView.invalidate();

应该工作