位图周围的圆形边框

发布于 2024-12-11 16:59:00 字数 813 浏览 0 评论 0原文

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
    bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = 12;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

我正在尝试使用此代码来舍入位图,但我不知道 Mode.SRC_in 和 Config.ARGB_8888 是什么。我对他们有错误。我应该在这里做什么?

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
    bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = 12;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

I am trying use this code for rounding bitmap, but I don't what is Mode.SRC_in and Config.ARGB_8888. I have error with them. What should I do here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光无声 2024-12-18 16:59:00

对于PorterDuffXfermode,你必须写import android.graphics.PorterDuffXfermode;

对于Config.ARGB_8888,你必须写import android.graphics.Bitmap.Config;

否则直接按< kbd>CTRL + SHIFT + O 组织导入。

For PorterDuffXfermode, you have to write import android.graphics.PorterDuffXfermode;

For Config.ARGB_8888, you have to write import android.graphics.Bitmap.Config;

Otherwise Direct press CTRL + SHIFT + O to organize imports.

治碍 2024-12-18 16:59:00

制作了一个椭圆形 xml,将其命名为 round_shape.xml

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

将此 xml 设置为图像视图的背景,如下所示

    <ImageView
                android:id="@+id/img_profile"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:padding="2dp"
                android:scaleType="fitXY"
                android:background="@drawable/round_shape"/>

现在您已将位图舍入并设置为 imagebitmap 资源,如下所示this img_profile.setImageBitmap(roundBit(selected_Pic_Bitmap)); ,对于舍入图像位图,请使用下面的代码

public Bitmap roundBit(Bitmap bm) {

    Bitmap circleBitmap = Bitmap.createBitmap(bm.getWidth(),
            bm.getHeight(), Bitmap.Config.ARGB_8888);

    BitmapShader shader = new BitmapShader(bm, TileMode.CLAMP,
            TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setShader(shader);
    paint.setAntiAlias(true);
    Canvas c = new Canvas(circleBitmap);

    c.drawCircle(bm.getWidth() / 2, bm.getHeight() / 2, bm.getWidth() / 2,
            paint);

    return circleBitmap;
}

round_image_with_border

made a oval shape xml name it to round_shape.xml

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

Set this xml to background of image view like below

    <ImageView
                android:id="@+id/img_profile"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:padding="2dp"
                android:scaleType="fitXY"
                android:background="@drawable/round_shape"/>

Now you have round the bitmap and set as imagebitmap resource like this img_profile.setImageBitmap(roundBit(selected_Pic_Bitmap)); , For rounding image bitmap use below code

public Bitmap roundBit(Bitmap bm) {

    Bitmap circleBitmap = Bitmap.createBitmap(bm.getWidth(),
            bm.getHeight(), Bitmap.Config.ARGB_8888);

    BitmapShader shader = new BitmapShader(bm, TileMode.CLAMP,
            TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setShader(shader);
    paint.setAntiAlias(true);
    Canvas c = new Canvas(circleBitmap);

    c.drawCircle(bm.getWidth() / 2, bm.getHeight() / 2, bm.getWidth() / 2,
            paint);

    return circleBitmap;
}

round_image_with_border

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文