android - 在矩形外填充 Canvas

标签 android canvas

我想填充 Canvas 上矩形以外的区域。我用

 canvas.drawRect(pTopLeft.x, pTopLeft.y, pBotRight.x, pBotRight.y, paint);

绘制矩形,但无法弄清楚如何在矩形/剪辑之外进行填充。

谢谢 杰夫

最佳答案

感谢 ted 和 trojanfoe - 我想出的最巧妙的解决方案是

    Point pTopLeft = new Point();
    Point pBotRight = new Point();

    //TODO:set x,y for points

    Rect rHole = new Rect(pTopLeft.x, pTopLeft.y, pBotRight.x, pBotRight.y);
    //assume current clip is full canvas
    //put a hole in the current clip
    canvas.clipRect(rHole,  Region.Op.DIFFERENCE);
    //fill with semi-transparent red
    canvas.drawARGB(50, 255, 0, 0);
    //restore full canvas clip for any subsequent operations
    canvas.clipRect(new Rect(0, 0, canvas.getWidth(), canvas.getHeight())
                    , Region.Op.REPLACE);

关于android - 在矩形外填充 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4998389/

相关文章:

android - 如何仅使用 CLI 在 Linux 上安装 Android SDK?

android - 如何将 Drawable 转换为缩放位图

javascript - 如何在 html 5 Canvas 上旋转单个对象?

javascript - 将多边形线拟合到 Canvas 中绘制的像素的算法?

javascript - 如何将 Canvas 转换为图像以上传到 flask ?

java - 在哪里可以找到由 eclipse 创建的 android keystore 的默认位置?

android - 如何在 Android ScrollEvent 上有选择地重绘 View ?

java - 如何在 Android 中获取位图的 VM 预算?

javascript - 这段代码有什么问题? HTML5-Canvas

javascript - 在 FabricJS 中绘制一条波浪线