0

trying to make a rectangular ShapeDrawable that has a rectangular cutout - it needs to actually be cutout so that the middle is 'transparent', it can't be a white rectangle on top of a larger nonwhite one or something like that.

Here's what I have so far (all that can be seen however is the top 'bar' of the rectangle, which oddly enough is black despite that being the color of the cutout):

    PorterDuffColorFilter pd = new PorterDuffColorFilter(Color.BLACK, PorterDuff.Mode.SRC_OUT);
    ShapeDrawable cutout = new ShapeDrawable();
    cutout.setBounds(top.x, top.y, top.x+cellLength*matCols, top.y+cellLength*matRows);
    cutout.setColorFilter(pd);
    ShapeDrawable border = new ShapeDrawable();
    border.setBounds(Math.round(top.x-thick*cellLength), Math.round(top.y-thick*cellLength), Math.round(top.x+cellLength*(matCols+thick)), Math.round(top.y+cellLength*(matRows+thick)));
    LayerDrawable ogre = new LayerDrawable(new Drawable[]{cutout, border});
    Button test = new Button(this.getContext());
    test.setLayoutParams(new LayoutParams((int)Math.round(cellLength*(matCols + 2*thick)), (int)Math.round(cellLength*thick)));
    test.setBackground(ogre);
4

2 回答 2

0

您可以使用 PathShape 代替我们可绘制对象中的默认矩形。将两个具有相反路径方向的矩形添加到路径中,以便从另一个中切出一个。

或者,您可以将边框划分为不与剪切区域重叠的顶部、底部、左侧和右侧矩形。

于 2018-10-10T17:01:31.347 回答
0

为什么不创建一个可绘制的 xml:

矩形.xml

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="4dp" android:color="#000000"></stroke>
</shape>

并像这样使用它:

test.setBackgroundResource(R.drawable.rect);
于 2018-10-09T21:06:13.820 回答