1

在我的应用程序中,我有一些图像,当我点击该图像时,它应该旋转 90 度。我可以旋转一次图像,但不能在第二次点击时旋转。谁能帮我解决这个问题?如何在每个触摸事件上旋转图像?

if (event.getAction() == MotionEvent.ACTION_DOWN) {
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.quartercircle1);
        Matrix m = new Matrix();
        imgvwQrtr1.setScaleType(ScaleType.MATRIX);

        m.setRotate(90f, imgvwQrtr1.getDrawable().getBounds().width()/2, imgvwQrtr1.getDrawable().getBounds().height()/2);
        bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
        imgvwQrtr1.setImageBitmap(bm);


        ClipData data = ClipData.newPlainText("", "");
        DragShadowBuilder shadowBuilder = new DragShadowBuilder(v);
        v.startDrag(data, shadowBuilder, v, 0);
        return true;
    }
4

2 回答 2

1

您需要创建全局变量,例如:

int degree = 0;

并在代码中

.....
//shortest way
degree = degree + 90;
if(degree % 360 == 0) {
   degree = 0;
}
//if(degree >= 270) {
   //degree = 0;
//} else {
   //degree+=90;
//}
m.setRotate(degree, imgvwQrtr1.getDrawable().getBounds().width()/2,
imgvwQrtr1.getDrawable().getBounds().height()/2);
....
于 2013-01-23T13:37:04.097 回答
0

试试这个

    public static float getRotationAngle(final float x1, final float y1, final float x2, final float y2) {
                final float CLOCK_WISE = 1.0f;
                final float ANTI_CLOCK_WISE = -1.0f;

                final float deltaX = Math.abs(x2 - x1);
                final float deltaY = Math.abs(y2 - y1);

                if (deltaX == 0) {
                        return 0.0f;
                }

                final float angle = (float) Math.toDegrees(Math.atan(deltaY / deltaX));

                if (x1 <= x2 && y2 <= y1) {
                        return CLOCK_WISE * (90.0f - angle);
                } else if (x2 <= x1 && y2 <= y1) {
                        return ANTI_CLOCK_WISE * (90.0f - angle);
                } else if (x2 <= x1 && y1 <= y2) {
                        return ANTI_CLOCK_WISE * (90.0f + angle);
                } else if (x1 <= x2 && y1 <= y2) {
                        return CLOCK_WISE * (90.0f + angle);
                } else {
                        return 0.0f;
                }
        }
于 2013-01-23T13:37:53.940 回答