简体   繁体   English

圆角边框到位图

[英]rounded border around to bitmap

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. 我正在尝试使用此代码舍入位图,但我不是Mode.SRC_in和Config.ARGB_8888。 I have error with them. 我对他们有误。 What should I do here? 我应该在这里做什么?

made a oval shape xml name it to round_shape.xml 将椭圆形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>     

Set this xml to background of image view like below 将此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"/>

Now you have round the bitmap and set as imagebitmap resource like this img_profile.setImageBitmap(roundBit(selected_Pic_Bitmap)); 现在,您已经对位图进行了舍入并设置为imagebitmap资源,例如img_profile.setImageBitmap(roundBit(selected_Pic_Bitmap)); , For rounding image bitmap use below code ,对于四舍五入的图像位图,请使用以下代码

public Bitmap roundBit(Bitmap bm) { 公开位图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

For PorterDuffXfermode, you have to write import android.graphics.PorterDuffXfermode; 对于PorterDuffXfermode,您必须编写import android.graphics.PorterDuffXfermode; ;。

For Config.ARGB_8888, you have to write import android.graphics.Bitmap.Config; 对于Config.ARGB_8888,您必须编写import android.graphics.Bitmap.Config; ;。

Otherwise Direct press CTRL + SHIFT + O to organize imports. 否则,直接按CTRL + SHIFT + O来组织导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM