如何从方形customview android中删除部分画布?

时间:2017-05-04 10:29:32

标签: android canvas background android-custom-view transparent

enter image description here我一直试图在弧顶上绘制一个填充颜色的圆弧,并在自定义视图矩形中移除圆弧的底部。我经历了很多stackoverflow问题,但它不起作用。需要从下面的屏幕截图中删除画布中的白色区域。自定义视图的代码如下所示。请帮我弄清楚解决方案。感谢

代码:

@Override
protected void onDraw(Canvas canvas) {
    float size = Math.min(getWidth(), getWidth());
    RectF rectRectF = new RectF(0, 0, getWidth(), getHeight());
    Path rectPath = new Path();
    rectPath.addRect(rectRectF, Path.Direction.CCW);
    Paint bgPaint = new Paint();
    bgPaint.setColor(Color.RED);
    canvas.drawPath(rectPath, bgPaint);
    RectF rectF = new RectF(0, 0, 2 * getWidth(), 2 * getWidth());
    Paint transparentPaint = new Paint();
    transparentPaint.setColor(Color.WHITE);
    transparentPaint.setAntiAlias(true);
    canvas.drawArc(rectF, 180, 90, true, transparentPaint);

}

1 个答案:

答案 0 :(得分:2)

使用

 private Canvas bitMapCanvas;
    private Bitmap frameBitmap;
    private Paint paint;
    private PorterDuffXfermode porterDuffXfermode;
     @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // first time setup
        if (bitMapCanvas == null) {
          bitMapCanvas = new Canvas();
          frameBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
          bitMapCanvas.setBitmap(frameBitmap);
     porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);

        }
        super.onSizeChanged(w, h, oldw, oldh);
      }

 @Override
  protected void onDraw(Canvas canvas) {
.....
....
  paint.setXfermode(porterDuffXfermode);
    bitMapCanvas.drawArc(rectF, 180, 90, true, paint);
canvas.drawBitmap(frameBitmap, 0, 0, null);
  }

这可以帮到你。它会清除你的颜色并使其透明