更改图像为圆角 [英] Changing Image as Rounded Corner

查看:137
本文介绍了更改图像为圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1的ListView有一些项目。在我的列表视图我一直使用自定义适配器,用于图像显示的物品。我的项目中的图像是从 JSON 未来我的图片都是这样 -

现在,我只需要带圆角的图像。我如何实现这一目标?

解决方案

 公共静态位图getRoundedCornerBitmap(位图位图){
    位图输出= Bitmap.createBitmap(bitmap.getWidth()
        bitmap.getHeight(),Config.ARGB_8888);
    帆布油画=新的Canvas(输出);

    最终诠释色= 0xff424242;
    最终的涂料粉刷=新的油漆();
    最终矩形矩形=新的Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
    最后RectF rectF =新RectF(RECT);
    最终浮动roundPx = 12;

    paint.setAntiAlias​​(真正的);
    canvas.drawARGB(0,0,0,0);
    paint.setColor(颜色);
    canvas.drawRoundRect(rectF,roundPx,roundPx,油漆);

    paint.setXfermode(新PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(位图,矩形,矩形,油漆);

    返回输出;
  }
 

http://ruibm.com/?p=184

提取code

I've one listview with some items. In my listview i've using custom adapter for displaying the items with images. My images in items are coming from JSON My images are like this -

Now, I just need the image with rounded corner. How do i achieve this?

解决方案

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;
  }

code extracted from http://ruibm.com/?p=184

这篇关于更改图像为圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆