Android 如何制作圆形的ImageView?

Android 如何制作圆形的ImageView?,android,imageview,Android,Imageview,我想做那样的事 这是一个具有名称和用户图像的列表视图行 我已经做了一些搜索和图像循环,但不是完美的解决方案。 任何帮助都会帮助我 我的代码添加到图像加载器类 public Bitmap processBitmap(Bitmap bitmap) { int pixels = 0; if (mRound == 0) pixels = 120; else pixels = mRound; Bitmap output = Bitmap.c

我想做那样的事

这是一个具有名称和用户图像的列表视图行

我已经做了一些搜索和图像循环,但不是完美的解决方案。 任何帮助都会帮助我

我的代码添加到图像加载器类

public Bitmap processBitmap(Bitmap bitmap) {
    int pixels = 0;
    if (mRound == 0)
        pixels = 120;
    else
        pixels = mRound;
    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 = pixels;

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

谢谢。

这可能无法回答您的问题,但是,作为替代方案,您可以通过在
框架布局中使用两个图像视图来模拟这种效果,例如:一个在底部-这将是图片,一个在顶部-这将是一个灰色正方形,中间有一个圆,圆环的“主体”是透明的


这样,您就不必进行任何位图处理。但是,选择一个更适合您的需要。

您也可以使用此功能。。。这是我的工作

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
    int targetWidth = 50;
    int targetHeight = 50;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
                        targetHeight,Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2,
        ((float) targetHeight - 1) / 2,
        (Math.min(((float) targetWidth), 
        ((float) targetHeight)) / 2),
        Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, 
        new Rect(0, 0, sourceBitmap.getWidth(),
        sourceBitmap.getHeight()), 
        new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

谢谢你的回复

Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setStrokeWidth(5);
Canvas c = new Canvas(circleBitmap);
 //This draw a circle of Gerycolor which will be the border of image.
c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2, paint);
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setAntiAlias(true);
paint.setShader(shader);
// This will draw the image.
c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2-2, paint);


使用imageLoader github.com/nostra13/Android-Universal-image-Loader对图像进行取整 创建一个选项

 DisplayImageOptions options = new DisplayImageOptions.Builder()
 // this will make circle, pass the width of image 
.displayer(new RoundedBitmapDisplayer(getResources().getDimensionPixelSize(R.dimen.image_dimen_‌​menu))) 
.cacheOnDisc(true) 
.build(); 
imageLoader.displayImage(url_for_image,ImageView,options);

或者你可以从squre使用毕加索图书馆

Picasso.with(mContext).load(URL+b.image)
 .placeholder(R.drawable.profile) 
    .error(R.drawable.profile) 
    .transform(new RoundedTransformation(50, 4)) 
    .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size) 
    .centerCrop() 
    .into(v.im_user);
您可以下载RoundedTrasInformation

重要: 我在使用UIL时发现了一个问题。如果没有将xml属性android:contentDescription=“TODO”放在ImageView中。然后UIL显示简单的图像。
希望所有人都能理解

它可以工作,请参见我的答案。使用此解决方案,您需要知道背景颜色,并且它必须是唯一的颜色,没有渐变或其他颜色。有关如何执行此操作的示例,请参见以下链接。请参考这些问题,特别是这个答案,点击它们会帮助你实现你想要的东西。试试这个检查:使用imageLoader创建一个圆形图像选项;DisplayImageOptions=new DisplayImageOptions.Builder()//这将形成一个圆,传递image.displayer的宽度(new RoundedBitmapDisplayer(getResources().getDimensionPixelSize(R.dimen.image_dimen_菜单))).cacheOnDisc(true.build();displayImage(url\u用于图像、图像视图、选项);或者你可以从squre使用毕加索图书馆。毕加索.with(mContext).load(com.app.utility.Constants.BASE\u URL+b.image).占位符(R.drawable.profile).错误(R.drawable.profile).转换(新一轮转换(50,4)).resizeDimen(R.dimen.list\u detail\u image\u size,R.dimen.list\u detail\u image\u size).centerCrop().转换为(v.im\u用户);你可以在这里下载RoundedTrasformation文件@GalRom。这个文件很有魅力。谢谢你的这张照片。如何增加圆的形状。如何添加白色边框?这很好,但我在圆的图像上有一个黑色的矩形背景。我得到了圆的图像…但是表面不光滑…有人能告诉我解决方法吗?