【问题标题】:How set clip path for children only?如何为儿童设置剪辑路径?
【发布时间】:2018-02-13 08:22:05
【问题描述】:

我有一个扩展RelativeLayout 的自定义视图。 这有很多孩子。 我只想要剪辑孩子,但我的代码也剪辑了我的自定义视图。

public class LoadingBox extends RelativeLayout
{

    @Override
    protected void dispatchDraw(Canvas canvas)
    {
        save = canvas.save();
          canvas.getClipBounds(clipRect);
          clipRectF.set(clipRect.left, clipRect.top, clipRect.right, 
          clipRect.bottom);
          clipPath.addRoundRect(clipRectF, 40f, 40f, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.dispatchDraw(canvas);
        canvas.restoreToCount(save);
    }

} 

我测试过

@Override
protected void onDraw(Canvas canvas){...} 

也是,但我再次接受这个结果。

我如何只剪辑孩子而不是自己的视图?

【问题讨论】:

  • 请多说一些(先画图再保存= canvas.save();)
  • 现在更糟了,可以写出你的想法

标签: java android draw android-custom-view clip


【解决方案1】:

我找到了答案。 我用下面的代码

 LayoutInflater layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 ViewGroup layout = (ViewGroup) layoutInflater.inflate(R.layout.loading_box, this);

这是错误的, 必须动态创建子视图并覆盖drawChild

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime)
{
    boolean result;
    canvas.save();
    clipPath.reset();
    canvas.getClipBounds(clipRect);
    clipRectF.set(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
    clipPath.addRoundRect(clipRectF, radius-2, radius-2, Path.Direction.CW);
    canvas.clipPath(clipPath);
    result = super.drawChild(canvas, child, drawingTime);
    canvas.restore();
    return result;
}

【讨论】:

    猜你喜欢
    • 2017-10-24
    • 2021-11-30
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多