Java android删除一个矩形并绘制其余的图像

时间:2017-03-09 23:23:34

标签: java android android-bitmap

我正在尝试从位图裁剪矩形并在画布上绘制其余图像。到目前为止可以实现。请问你能帮帮忙吗?代码如下 -

1. //draw activity on the canvas. this returns a bitmap used in Bitmap.createBitmap(3)
 activity.getWindow().getDecorView().draw(canvas);

2. //get the coordinates that I want to clip.
 Rect removeImage = new rect(coordinates of rect portion)

3. //check that I can get the cropped image 
Bitmap croppedImage = Bitmap.createBitmap(bitmap, leftCrop, topCrop,    rightCrop, bottomCrop);

4. //Now I want to get the remaining canvas and remove the rectangular region 
?????????

1 个答案:

答案 0 :(得分:0)

为了从活动的位图中删除矩形部分,我们可以使用以下代码 -

    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), ARGB_8888);        
    Canvas canvas = new Canvas(bitmap);
    Paint p = new Paint();
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
    activity.getWindow().getDecorView().draw(canvas);
    canvas.drawRect(rect,p);
    canvas.save();

您可以手动计算矩形坐标或提供您的矩形坐标。我通过了视图并使用了以下内容 -

    Rect coordinates = new Rect();
    int[] xyLocation = new int[2];
    if (view == null) {
        return coordinates;
    }
    view.getLocationOnScreen(xyLocation);
    coordinates.left = xyLocation[0];
    coordinates.top = xyLocation[1];
    coordinates.right = coordinates.left + view.getWidth();
    coordinates.bottom = corrdinates.top + view.getHeight();
    return coordinates;

removed rectangle from activity screen shot