如何将ImageView制作成圆形?

28

我想做类似于这样的事情。

列表视图中的图像

这是一个包含用户名和用户图像的列表视图行。

我已经尝试过将图像变成圆形,但并不是完美的解决方案。希望能得到帮助。

我的代码添加到了Image Loader类中。

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

谢谢。


1
请参考以下问题:stackOverflow - question 1 stackOverflow - question 2 stackOverflow - question 3,特别是这个答案 stackOverflow - answer 1,它们会对你实现想要的功能有很大帮助。 - Ahmed Ekri
3
试试这个:https://dev59.com/Rm025IYBdhLWcg3wpXse#5882430 - Ritesh Gune
2个回答

10

这可能不能完全回答你的问题,但是,作为另一种选择,你可以通过在FrameLayout中使用2个ImageView来模仿该效果:一个在底部 - 这将是图片,另一个在顶部 - 这将是一个带有圆形的灰色正方形,并且圆形的"身体"是透明的。

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


1
使用此解决方案,您需要知道背景颜色,并且它必须是没有渐变或其他任何东西的唯一颜色。 - Stephane Mathis
请查看以下链接,了解如何执行此操作。http://www.techrepublic.com/article/pro-tip-round-corners-on-an-android-imageview-with-this-hack/ - rharvey

6

感谢您的回复,这是通过技术实现的。

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

或者您可以使用来自Square的Picasso库。
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);

您可以在此处下载RoundedTransformation文件
重要提示:当使用UIL时,我发现一个问题。如果您没有将xml属性android:contentDescription = “TODO”放入ImageView中,则UIL会显示简单的图像。希望大家都能理解。

2
请查看以下链接:http://androidtutorialbd.blogspot.co.il/2013/08/circle-imageview-extend-imageview.html?showComment=1399528351106#c6416608644566007267 - Gal Rom
2
圆形图片使用imageLoader https://github.com/nostra13/Android-Universal-Image-Loader创建一个Options; DisplayImageOptions options = new DisplayImageOptions.Builder() // 这将制作圆形,传递图像的宽度 .displayer(new RoundedBitmapDisplayer(getResources().getDimensionPixelSize(R.dimen.image_dimen_menu))) .cacheOnDisc(true) .build();imageLoader.displayImage(url_for_image,ImageView,options); - shailesh
1
你可以使用Square的Picasso库。Picasso.with(mContext) .load(com.app.utility.Constants.BASE_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);你可以在这里下载RoundedTrasformation文件 https://www.dropbox.com/s/lp3d43hra3gbhul/RoundedTransformation.java - shailesh
@GalRom 这个非常好用。谢谢你的分享。 - jayellos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接