Skip to content

Instantly share code, notes, and snippets.

@isansc
Forked from zmeid/CircularNetworkImageView
Created November 3, 2017 20:36
Show Gist options
  • Save isansc/3552efadaaf03f47d211d8c1d91abf98 to your computer and use it in GitHub Desktop.
Save isansc/3552efadaaf03f47d211d8c1d91abf98 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment