Skip to content

Instantly share code, notes, and snippets.

@zmeid
Forked from bkurzius/CircularNetworkImageView
Last active July 17, 2018 08:29
Show Gist options
  • Save zmeid/b36f94237dadd7242b3343db92e86376 to your computer and use it in GitHub Desktop.
Save zmeid/b36f94237dadd7242b3343db92e86376 to your computer and use it in GitHub Desktop.
The original "CircularNetworkImageView.java" class is not well organized to use in recyclerview or listview. It calculates different sizes for each image. I have edited the "getCircularBitmap" method of original class, in order to get same-sized and same-positioned circular imageviews in recyclerview, listview.
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import com.android.volley.toolbox.NetworkImageView;
public class CircularNetworkImageView extends NetworkImageView {
Context mContext;
public CircularNetworkImageView(Context context) {
super(context);
mContext = context;
}
public CircularNetworkImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
mContext = context;
}
public CircularNetworkImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
mContext = context;
}
@Override
public void setImageBitmap(Bitmap bm) {
if(bm==null) return;
setImageDrawable(new BitmapDrawable(mContext.getResources(),
getCircularBitmap(bm)));
}
/**
* Creates a circular bitmap and uses whichever dimension is smaller to determine the width
* <br/>Also constrains the circle to the leftmost part of the image
*
* @param bitmap
* @return bitmap
*/
public Bitmap getCircularBitmap(Bitmap bitmap) {
int size = Math.min(bitmap.getWidth(), bitmap.getHeight());
Bitmap output = Bitmap.createBitmap(size,
size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
BitmapShader shader;
shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
RectF rect = new RectF(0, 0 ,size,size);
int radius = size/2;
canvas.drawRoundRect(rect, radius,radius, paint);
return output;
}
}
@ishumaurya
Copy link

hey just explain me where to put this file???

@isansc
Copy link

isansc commented Nov 3, 2017

Man, very assertive and simple! Congratulations! Loved your solution! Thank you!

@shubh261096
Copy link

shubh261096 commented Jul 17, 2018

Great Man, It worked perfectly with RecyclerView. Can you please provide with the rounded corners imageView also?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment