Skip to content

Instantly share code, notes, and snippets.

@raghunandankavi2010
Created March 11, 2017 09:53
Show Gist options
  • Save raghunandankavi2010/d065779343fccd4e52da0924863221c2 to your computer and use it in GitHub Desktop.
Save raghunandankavi2010/d065779343fccd4e52da0924863221c2 to your computer and use it in GitHub Desktop.
RoundImageView
public class RoundImageview extends NetworkImageView {
Context mContext;
public RoundImageview(Context context) {
super(context);
mContext = context;
}
public RoundImageview(Context context, AttributeSet attrs) {
this(context, attrs, 0);
mContext = context;
}
public RoundImageview(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) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int width = bitmap.getWidth();
if(bitmap.getWidth()>bitmap.getHeight())
width = bitmap.getHeight();
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, width, width);
final RectF rectF = new RectF(rect);
final float roundPx = width / 2;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment