具有子坐标的Android裁剪父位图

时间:2014-02-05 06:27:08

标签: android

我有两个ImageView / Bitmap,一个用于可见背景,一个不可移动一个,另一个是背景视图上的可移动位图。可移动位图具有捏缩放和矩阵旋转。最后使用那些矩形位置找到可移动的坐标/矩形位置和裁剪背景位图。我的问题是如何计算/获得直线位置?。

对于我的样本,我需要你的观点。请在这里发布您的想法,它对每个人都有帮助。

enter image description here

1 个答案:

答案 0 :(得分:1)

我花了更多时间进行搜索,并在几周后获得此结果。

private Bitmap getCroppedBmp(Bitmap childBmp, Bitmap ParentBmp) {
    // viewBmp is movable bitmap, orgBitmap is Parent Bitmap
    Bitmap viewBmp = childBmp;
    Bitmap orgBitmap = ParentBmp;

    float[] v = new float[9];
    // get Matrix from View
    cropImage.savedMatrix.getValues(v); 

    float left = v[Matrix.MTRANS_X];
    float top = v[Matrix.MTRANS_Y];

    // calculate the degree of rotation
    float rAngle = Math.round(Math.atan2(v[Matrix.MSKEW_X],
            v[Matrix.MSCALE_X]) * (180 / Math.PI));

    // Rotate viewBmp
    Matrix m = new Matrix();
    m.postRotate(rAngle, viewBmp.getWidth() / 2, viewBmp.getHeight() / 2);
    Bitmap bmap = Bitmap.createBitmap(viewBmp, 0, 0, viewBmp.getWidth(),
            viewBmp.getHeight(), m, true);

    // calculate real scale
    float rScale = (float) Math
            .sqrt((v[Matrix.MSCALE_X] * v[Matrix.MSCALE_X])
                    + (v[Matrix.MSKEW_Y] * v[Matrix.MSKEW_Y]));

    // Scale viewBmp
    Matrix m1 = new Matrix();
    m1.postScale(rScale, rScale, width / 2, height / 2);
    Bitmap scaledBitmap = Bitmap.createBitmap(bmap, 0, 0, width, height,
            m1, true);
    // Finally Get cropped Bitmap
    Bitmap resultingImage = Bitmap.createBitmap(orgBitmap, (int) left,
            (int) top, scaledBitmap.getWidth(), scaledBitmap.getHeight());
    return resultingImage;
}