位图周围的圆形边框
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于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.
制作了一个椭圆形 xml,将其命名为 round_shape.xml
将此 xml 设置为图像视图的背景,如下所示
现在您已将位图舍入并设置为 imagebitmap 资源,如下所示this img_profile.setImageBitmap(roundBit(selected_Pic_Bitmap)); ,对于舍入图像位图,请使用下面的代码
public Bitmap roundBit(Bitmap bm) {
made a oval shape xml name it to round_shape.xml
Set this xml to background of image view like below
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) {