How to Make an ImageView in Circular Shape? [duplicate]

南笙酒味 提交于 2019-11-26 03:53:55

问题


I want to do something like that.

\"image

This is a list view row with name and the image of user.

I have done some searching and have done the image circular,but not the perfect solution. Any help will be helping me.

my code added to the Image Loader class

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

Thanks.


回答1:


you can use this function also ... Its work for me..

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



回答2:


This probably won't answer your question, but, as an alternative you could mimic that effect by having 2 ImageViews in a FrameLayout for example: one at the bottom - this will be the picture, and one on top - this will be the a gray square with a circle in middle, and the "body" of cirle to be transparent.

This way you won't have to do any bitmap processing. But, choose the one that suits better for your needs.




回答3:


This is accomplished thanks for the reply.

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

Rounded image Using imageLoader github.com/nostra13/Android-Universal-Image-Loader Create a Options;

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

Or U can user Picasso Library from 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);

you can download RoundedTrasformation file here

IMPORTANT: i found one issue when using UIL. If you didn't put the xml attribute android:contentDescription="TODO" in ImageView. Then UIL show simple image. Hope all understand



来源:https://stackoverflow.com/questions/18378741/how-to-make-an-imageview-in-circular-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!