如何使用setPolyToPoly旋转矩阵

时间:2016-01-27 01:40:56

标签: android android-canvas

我是android类和开发的新手。我正在绘制一个画布图像,我在setPolyToPoly上进行自由转换。但我也想让它旋转或缩放并移动。我很困惑,也不知道在哪里放置代码。

情况是这样的。我可以移动,旋转和缩放图像。但是当我进行转换然后移动图像时,setPolyToPoly中的转换将被重置。如何让它不被重置。感谢

r = new RectF(); 
matrix.mapRect(r);
float[] src2 = new float[] { 
                0, 
                0,
                bitmapFreeTrans.getWidth(), 
                0, 
                bitmapFreeTrans.getWidth(), 
                bitmapFreeTrans.getHeight(), 
                0, 
                bitmapFreeTrans.getHeight() };
        float[] dst2 = new float[] { 
                CIRCLE_TOP_LEFT.x,
                CIRCLE_TOP_LEFT.y,
                CIRCLE_TOP_RIGHT.x, 
                CIRCLE_TOP_RIGHT.y,
                CIRCLE_BOTTOM_RIGHT.x,
                CIRCLE_BOTTOM_RIGHT.y, 
                CIRCLE_BOTTOM_LEFT.x,
                CIRCLE_BOTTOM_LEFT.y 
                };
        canvas.save();
        matrix.setPolyToPoly(src2, 0, dst2, 0, 4);

以下是我的手势监听器:

 SimpleOnMoveGestureListener mgl = new SimpleOnMoveGestureListener() {
    @Override
    public boolean onMove(MoveGestureDetector detector) {
        PointF delta = detector.getFocusDelta();
        matrix.postTranslate(delta.x, delta.y);
        parent.invalidate();
        x1 = delta.x;
        y1 = delta.y;
        return true;
    }
};

SimpleOnScaleGestureListener sgl = new SimpleOnScaleGestureListener() {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        scaleFactor = detector.getScaleFactor();
        matrix.postScale(scaleFactor, scaleFactor, detector.getFocusX(), detector.getFocusY());
        parent.invalidate();
        // copy values for rotate gesture
        getFocusX = detector.getFocusX();
        getFocusY = detector.getFocusY();
        return true;
    }
};

SimpleOnRotateGestureListener rgl = new SimpleOnRotateGestureListener() {
    @Override
    public boolean onRotate(RotateGestureDetector detector) {
        matrix.postRotate(-detector.getRotationDegreesDelta(), getFocusX, getFocusY);  
        parent.invalidate();
        return true;
    };
};

1 个答案:

答案 0 :(得分:0)

如果您使用的是位图,则可以

public Bitmap rotateBitmap(Bitmap source, float angle)
{
    //Rotates Bitmap
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);

您可以通过调用此方法来将现有的Bitmap设置为它....

myBitmap = rotateBitmap(myBitmap, 90);

这会将它旋转90度