Android转换路径使用Matrix显示“正常”

时间:2013-11-13 19:41:22

标签: android matrix path

我在Android中创建了一个Path。我还不能按需要显示此路径。当我显示路径时,它会显示从左上角开始的0,0坐标,并在屏幕上显示正y值。我希望显示路径,好像坐标系在左下角开始,y值增加,正如使用“普通”笛卡尔坐标所预期的那样。

我想知道什么是Matrix转换,我需要在Path上执行此操作。

从逻辑上讲,似乎180度“翻转”和90度旋转应该可以实现这一点。所以我做了一些测试,并且模式看起来有点像-90歪斜和90度旋转。我现在遇到的问题是,在倾斜操作之后,线条不再是直线。这让我相信我的倾斜转变可能会消失。

是否可以使用路径和矩阵使线条显示为直线且处于所需方向?如果是这样,怎么样?

public class MainActivity extends Activity {

private static final boolean DEBUG = true;
private static final String TAG = "MainActivity";

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

private void initLayout() {
    final ImageView iv = (ImageView) findViewById(R.id.imageView1);

    // run the sampleCase after the View has been laid-out
    iv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            iv.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            // run sampleCase
            sampleCase(iv);

        }
    });
}

public void sampleCase(ImageView iv){
    Path path = getInitialPath();

//      drawPath(iv, path);

    drawPath(iv, reorientPath(iv, path));
}

public Path reorientPath(View view, Path path){
    Matrix matrix = new Matrix();

    // skew -90
    matrix.setSkew(-90f, -90f);
    path.transform(matrix);
    matrix.reset();

    // rotate 90 CW
    matrix.setRotate(90f);
    path.transform(matrix);

    path = translatePathToView(path);
    path = scalePathToView(view, path);

    return path;
}

private Path translatePathToView(Path path){
    // moves the path into the visible field
    RectF bounds = getBounds(path);
    float xTrans = 0f - bounds.left;
    float yTrans = 0f - bounds.top;

    Matrix matrix = new Matrix();
    matrix.setTranslate(xTrans, yTrans);
    path.transform(matrix);
    return path;
}


private Path scalePathToView(View view, Path path){
    RectF bounds = getBounds(path);

    float viewW = view.getWidth();
    float viewH = view.getHeight();

    float pathW = bounds.right - bounds.left;
    float pathH = bounds.bottom - bounds.top;

    float scaleFactor = Math.min(viewW/pathW, viewH/pathH);

    Matrix matrix = new Matrix();
    matrix.setScale(scaleFactor, scaleFactor);
    path.transform(matrix);

    return path;
}

public RectF getBounds(Path path){
    RectF bounds = new RectF();
    path.computeBounds(bounds, false);
    if (DEBUG){
        Log.d(TAG, "bounds.bottom: " + bounds.bottom);
        Log.d(TAG, "bounds.top: " + bounds.top);
        Log.d(TAG, "bounds.left: " + bounds.left);
        Log.d(TAG, "bounds.right: " + bounds.right);
    }
    return bounds;
}

public Path getInitialPath(){
    Path path = new Path();
    path.moveTo(50, 0);
    path.lineTo(90, 0);
    path.lineTo(100, 10);
    path.lineTo(100, 100);
    path.lineTo(30, 100);
    path.lineTo(0, 70);
    path.lineTo(0, 50);
    path.lineTo(50, 0);
    return path;
}

public void drawPath(ImageView iv, Path path){
    Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    iv.setImageBitmap(bitmap);
    Paint paint = setupPaint(2, Color.BLACK);
    canvas.drawPath(path, paint);
}

private Paint setupPaint(int width, int color) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setColor(color);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeWidth(width);
    return paint;
}

}

转型之前:

Before transformations

转型后,关闭但没有雪茄:

After transformations

1 个答案:

答案 0 :(得分:2)

为传递给Matrix.setSkew()的浮点值添加更多精度,可以得到所需的结果。

matrix.setSkew(-180000f, -180000f);

现在,上,下,左,右线是直的。

Strait lines