28

I want to do something like that.

image in List view

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.

Sandeep Tiwari
  • 2,042
  • 3
  • 24
  • 47
shailesh
  • 1,783
  • 2
  • 17
  • 26
  • 3
    try this http://stackoverflow.com/a/5882430/1554935 – Ritesh Gune Aug 22 '13 at 11:15
  • 1
    Please refer to these questions [stackOverflow - question 1](http://stackoverflow.com/q/16922732/1932105) [stackOverflow - question 2](http://stackoverflow.com/q/16208365/1932105) [stackOverflow - question 3](http://stackoverflow.com/q/14179993/1932105) & specially this answer for clicking [stackOverflow - answer 1](http://stackoverflow.com/a/16926721/1932105) they're gonna help you a lot in achieving what you want. – Ahmed Ekri Aug 22 '13 at 11:14

2 Answers2

10

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.

Andy Res
  • 15,963
  • 5
  • 60
  • 96
  • 1
    With this solution you need to know the background color and it must be a unique color without gradient or anything. – Stephane Mathis May 16 '14 at 14:20
  • See the following link for an example on how to do this. http://www.techrepublic.com/article/pro-tip-round-corners-on-an-android-imageview-with-this-hack/ – rharvey Jul 24 '14 at 15:30
6

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

enter image description here

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

shailesh
  • 1,783
  • 2
  • 17
  • 26
  • 2
    check it out: http://androidtutorialbd.blogspot.co.il/2013/08/circle-imageview-extend-imageview.html?showComment=1399528351106#c6416608644566007267 – Gal Rom May 08 '14 at 05:53
  • 2
    Rounded image Using imageLoader https://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); – shailesh May 27 '14 at 09:16
  • 1
    Or U can user Picasso Library from squre. 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); you can download RoundedTrasformation file here https://www.dropbox.com/s/lp3d43hra3gbhul/RoundedTransformation.java – shailesh May 27 '14 at 09:17
  • @GalRom this one works like a charm. thank you for this one. – jayellos Oct 29 '14 at 06:34