如何在Android画布中剪切矩形

如何在Android画布中剪切矩形,android,canvas,clip,Android,Canvas,Clip,我创建了一个RoundRec canvas.drawRoundRectnew RectF0、0、100、10、7、7、油漆 但是我只想要顶部的两个圆角,所以我需要把底部的半个矩形0,0,0,5切出来,只留下顶部的半个边 我该怎么办?如果我理解正确,您想画一个只有顶角圆角的矩形吗 您可以使用xml创建一个 在res/drawable中,您将有一个xml,我们称之为myCustomRect,它将如下所示: <?xml version="1.0" encoding="utf-8"?> &l

我创建了一个RoundRec

canvas.drawRoundRectnew RectF0、0、100、10、7、7、油漆

但是我只想要顶部的两个圆角,所以我需要把底部的半个矩形0,0,0,5切出来,只留下顶部的半个边


我该怎么办?

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

您可以使用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"/>
我没有测试所有这些,因此您可能需要自己调试一点。

解决方案如下:

第二点:

Rect srcRect=new Rect0,0,origialBitmap.getWidth/2,origialBitmap.getHeight/2

应该是destRect

请记住,析构函数的坐标必须等于

0,0,destinationBitmap.getWidth,destinationBitmap.getHeight

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

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

因此,使用destinationBitmap代替originalBitmap更有意义,因为srcRect属于originalBitmap size/coords,destRect属于destinationBitmap size/coords

我的代码:

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。对于数值常数,我总是喜欢十六进制而不是十进制。在C/C++中,更多的fc*ng外来代码和更少的tha编译器工作,在Java中不知道:/

这是在TakePictureCameraSourceCallback中,用于在扫描条形码后绘制一个红色的矩形边框。 如果您正在构建类似的P.M.me,我将为您提供所有活动/片段代码,以使用Google Vision获取条形码,您可以在GitHub中搜索它,有一个使用CameraSource实例显示条形码周围的边界,直到您扫描它。 无论如何,我已经实现了一个直接返回切割条形码位图的活动,所以如果你不想做所有的工作,就pm。我不会要曼尼兹:P

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

Dlr=sqrt sqrx1-x2+sqry1-y2

你知道sqrt->平方根和sqr->Square so^2 so x1* x1

如果你只有1个坐标,比如Rect的工作原理,那意味着u 有4个值,左、右、上、下都是线,所以 你需要做左右距离的绝对值 顶部/底部的差异相同,ecc:

数学,左-右

请记住,Rect对坐标使用浮点值,因此需要 到Math.roundleft,并可能添加1,因为Math.round将舍入
你打算怎么处理这样一个物体?顺便说一句,你很幸运我没有看到这是一个重复的线程。
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;
}
ImageUtils.cutBitmap(bitmap, Math.round(rect.left), Math.round(rect.top), Math.round(rect.right), Math.round(rect.bottom));