1

我创建了一个 RoundRec。

canvas.drawRoundRect(new RectF(0, 0, 100, 10), 7, 7, paint);

但是我只想要上面的两个圆角,所以我需要把半底的 Rect(0, 0, 0, 5) 剪掉,只留下半顶边。

我应该怎么办?

4

2 回答 2

0

此处的解决方案: 如何剪切位图

第二:

矩形 srcRect = new Rect(0, 0, origialBitmap.getWidth() / 2, origialBitmap.getHeight() / 2);

应该是destRect

请记住,“destRect”的坐标必须等于

“0、0、destinationBitmap.getWidth()、destinationBitmap.getHeight()”

并且没有相同的“srcRect”坐标。链接中的示例有点奇怪,因为它使用“originalBitmap”来获取“destRect”,但“destRect”不属于“originalBitmap”,而是属于“destinationBitmap”(或“cutBitmap ”)。因此,如果要在所有“destinationBitmap”内绘制图片,则此“destRect”将具有:

左 = 0x0,上 = 0x0,右 = destinationBitmap.getWidth(),下 = destinationBitmap.getHeight()。

所以使用“destinationBitmap”代替“originalBitmap”更有意义,因为“srcRect”属于“originalBitmap”大小/坐标,“destRect”属于“destinationBitmap”大小/坐标。

我的代码:

Bitmap barcodeBitmap = Bitmap.createBitmap(Math.round(Math.abs(rect.left - rect.right)), Math.round(Math.abs(rect.top - rect.bottom)), Bitmap.Config.ARGB_4444);
        Canvas cutCanvas = new Canvas(barcodeBitmap);
        Rect srcRect = new Rect(Math.round(rect.left), Math.round(rect.top), Math.round(rect.right), Math.round(rect.bottom));
        Rect destRect = new Rect(0x0, 0x0, barcodeBitmap.getWidth(), barcodeBitmap.getHeight());
        cutCanvas.drawBitmap(bitmap, srcRect, destRect, null);

(对于那些不知道的人:0x0 = 0 in hexadecimal。对于数字常量,我总是更喜欢 hex 而不是 dec。更多的 fc*ng 异国代码和更少的工作为 tha 编译器(在 C/C++ 中,不知道这里爪哇:/))

这是在“TakePictureCameraSourceCallback”内,用于在您扫描条形码后绘制一个红色矩形边界。如果您正在构建类似的东西,请 PM 我,我会给您所有活动/片段代码以使用 Google Vision 获取条形码(您可以在 GitHub 中搜索它,有一个使用 CameraSource 实例来显示条形码周围的边界,直到您扫描它。无论如何,我已经实现了一个 Activity,它直接返回切割的条形码位图,所以如果你不想做所有的工作,只需 pm。我不会要求 maneyz :P)

你需要的数学: 如果你有 x/y 坐标的点,你可以通过执行以下操作获得左点和右点之间的距离:

Dlr = sqrt( sqr(x1 - x2) + sqr(y1 - y2) )

你知道“sqrt -> Square Root”和“sqr -> Square”所以“^2”所以“x1 * x1”。

如果您只有 1 个坐标,例如“矩形”的工作方式,这意味着您有 4 个值(左、右、上、下)是线条,因此要获得左右之间的距离,您需要执行绝对值差异(顶部/底部相同,ecc):

Math.abs(左 - 右)

请记住,“Rect”使用“float”值作为坐标,所以你需要“Math.round(left)”并且可能加 1,因为“Math.round”会将“<0.4”四舍五入为“0”,所以你会需要原始位图的“0.4”。

public static Bitmap cutBitmap(Bitmap originalBitmap, int srcLeft, int srcTop, int srcRight, int srcBottom){
    return cutBitmap(originalBitmap, new Rect(srcLeft, srcTop, srcRight, srcBottom));
}

public static Bitmap cutBitmap(Bitmap originalBitmap, Rect srcRect){
    Bitmap cutted = Bitmap.createBitmap(Math.abs(srcRect.left - srcRect.right), Math.abs(srcRect.top - srcRect.bottom), originalBitmap.getConfig());
    Canvas cutCanvas = new Canvas(cutted);
    Rect destRect = new Rect(0x0, 0x0, cutted.getWidth(), cutted.getHeight());
    cutCanvas.drawBitmap(originalBitmap, srcRect, destRect, null);
    return cutted;
}

这些是您可以在库中实现并调用以剪切您想要的图像的通用 Utils 函数。

所以上面关于条形码的代码变成了:

ImageUtils.cutBitmap(bitmap, Math.round(rect.left), Math.round(rect.top), Math.round(rect.right), Math.round(rect.bottom));

Byez,不错的 c0ding z3r0

于 2019-07-29T07:20:04.973 回答
-3

如果我理解正确,你想画一个只有顶角是圆角的矩形吗?

您可以使用 xml创建自定义形状。

在 res/drawable 中,您将拥有一个看起来像这样的 xml(我们称之为“myCustomRect”):

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

<corners
        android:topLeftRadius="7"
        android:topRightRadius="7"
        android:bottomLeftRadius="0"
        android:bottomRightRadius="0" />
<size
        android:width="100"
        android:height="10"/>
<solid
        android:color="#000000" />

</shape>

您将在布局中指定形状:

<ImageView android:id="@+id/myId"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:background="@drawable/muCustomRect"/>

我没有测试所有这些,所以你可能需要自己调试一下。

于 2011-05-05T16:30:25.067 回答