画布中有多个剪裁区域?

时间:2016-07-30 23:53:58

标签: android android-canvas

我想剪辑图片的多个区域, 所以我有一个扩展ImageView

的自定义类

这是我的onDraw:

@Override
protected void onDraw(Canvas canvas) {

        canvas.clipRect(rect1);
        canvas.clipRect(rect2);

    super.onDraw(canvas);

}

当我剪辑1个矩形时,它可以正常工作,但是当我有多个时,它根本不会显示任何内容。

更新

我还尝试使用Path剪切:

@Override
protected void onDraw(Canvas canvas) {

    canvas.clipPath(path1);
    canvas.clipPath(path2);

    super.onDraw(canvas);

}

但没有效果

2 个答案:

答案 0 :(得分:2)

我找到了解决方案,正在使用:

canvas.clipPath(Path path, Op op);

而不是:

canvas.clipPath(Path path);

使用额外剪辑,这也适用于RectRegion

我的代码如下:

Path path1 = new Path();

private void init(){

    path1.moveTo(100, 100);
    path1.lineTo(100, 500);
    path1.lineTo(500, 500);
    path1.lineTo(500, 100);
    path1.close();

}

@Override
protected void onDraw(Canvas canvas) {

    canvas.clipPath(path1);
    canvas.clipPath(path2, Region.Op.UNION);
    canvas.clipPath(path3, Region.Op.UNION);

    super.onDraw(canvas);

}

答案 1 :(得分:0)

它将在你的作品中交叉或替换。

请尝试使用clipPath(...)。

{
"_id": {
    "$oid": "579aa337f36d2808839a05e8"
},
"object_class": "Goods & Services Transaction",
"object_category": "Revenue",
"object_type": "Transaction",
"object_origin": "Sage One",
"object_origin_category": "Bookkeeping",
"object_creation_date": "2016-07-05T00:00:00.201Z",
"party_uuid": "dfa1e80a-5521-11e6-beb8-9e71128cae77",
"connection_uuid": "b945bd7c-7988-4d2a-92f5-8b50ab218e00",
"transaction_reference": "SI-1",
"transaction_status": "UNPAID",
"transaction_date": "2016-06-16T00:00:00.201Z",
"transaction_due_date": "2016-07-15T00:00:00.201Z",
"transaction_currency": "GBP",
"goods_and_services": [
    {
        "item_identifier": "PROD01",
        "item_name": "Product One",
        "item_quantity": 1,
        "item_gross_unit_sale_value": 1800,
        "item_revenue_category": "Sales Revenue",
        "item_net_unit_cost_value": null,
        "item_net_unit_sale_value": 1500,
        "item_unit_tax_value": 300,
        "item_net_total_sale_value": 1500,
        "item_gross_total_sale_value": 1800,
        "item_tax_value": 300
    }
],
"transaction_gross_value": 1800,
"transaction_gross_curr_value": 1800,
"transaction_net_value": 1500,
"transaction_cost_value": null,
"transaction_payments_value": null,
"transaction_payment_extras_value": null,
"transaction_tax_value": 300,
"party": {
    "customer": {
        "customer_identifier": "11",
        "customer_name": "KP"
    }
}
}

如果由于某种原因,clipPath不起作用,那么构建一个区域并使用clipRegion。

编辑:

根据OPs最终解决方案,还可以在添加剪辑区域时合并区域:

Path p1 = ...;
Path p2 = ...;
p2.addPath(p1);
canvas.clipRect(p2);