如何从画布中删除路径区域(Android) [英] How to erase path area from canvas (Android)

查看:408
本文介绍了如何从画布中删除路径区域(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要裁剪 ImageView 上的角。

似乎唯一的办法是重载 onDraw 方法,并使用 Path 从画布中擦除这些区域。问题是我没有纯色背景,所以我需要ERASE这些区域,但不填充一些颜色。

Seems like the only way to do that is to override onDraw method and erase these areas from canvas using Path. The problem is I have not solid color background, so I need ERASE these areas but not to fill them with some color.

我使用以下代码:

@Override
protected void onDraw(Canvas canvas) {
    Path path = new Path();
    path.moveTo(0, 0);
    path.lineTo(20, 0);
    path.lineTo(0, 20);
    path.close();

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawPath(path, paint);
    super.onDraw(canvas);
}

但是角落会变成黑色,你可以帮帮我吗?或者你可能知道我的任务更好的解决方案。

But the corner makes black but not transparent. Could you help me? Or probably you know better solution for my task. Here is how it looks like.

推荐答案

为了使用透明颜色绘制,必须使用 Paint setXfermode ,它只有在你的画布上设置一个位图时才能工作。

In order to draw with a transparent color you must use Paint setXfermode which will only work if you set a bitmap to your canvas. If you follow the steps below you should get the desired result.


  1. 创建画布并设置其位图。

  1. Create a canvas and set its bitmap.

mCanvas = new Canvas();
mBitmap= Bitmap.createBitmap(scrw, scrh, Config.ARGB_8888);
mCanvas.setBitmap(mBitmap);


  • 当您要删除某些内容时,只需要使用setXfermode。

  • When you want to erase something you just need to use setXfermode.

    if (isErasing)
       mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    else
       mPaint.setXfermode(null);
    


  • 现在您应该可以使用透明颜色绘制:

  • Now you should be able draw with a transparent color using:

    mCanvas.drawPath(yourpath,mPaint);

    这篇关于如何从画布中删除路径区域(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆