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。我和他们有错误。我该怎么办?
答案 0 :(得分:2)
对于PorterDuffXfermode,您必须编写import android.graphics.PorterDuffXfermode;
对于Config.ARGB_8888,您必须编写import android.graphics.Bitmap.Config;
否则直接按 CTRL + SHIFT + O 组织导入。
答案 1 :(得分:2)
将椭圆形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资源,就像这样 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;
}