如何从画布上抹去道区域(Android版) [英] How to erase path area from canvas (Android)

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

问题描述

我需要在的ImageView 裁剪角落。不要从各个角落四舍五入他们,但删除三角形。

I need to crop corners on ImageView. Not to round them but erase triangles from each corner.

好像这样做是重写的onDraw 方法,并利用帆布清除这些地区的唯一办法<​​code>路径 。问题是我没有纯色背景,所以我需要清除这些领域,但无法与某些颜色,以填补他们。

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.

我用下面的code为:

I use following code for that:

@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.

推荐答案

为了用透明色绘制您必须使用油漆 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天全站免登陆