Take a look in my example:
public class RoundedNetworkImageView extends NetworkImageView {
public RoundedNetworkImageView(Context context) {
super(context);
}
public RoundedNetworkImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundedNetworkImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable _drawable = getDrawable();
if (_drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap _bitmap = ((BitmapDrawable)_drawable)
.getBitmap().copy(Bitmap.Config.ARGB_8888, true);
canvas.drawBitmap(getCroppedBitmap(_bitmap, (int) (getWidth() / 1.2f)), 0,0, null);
}
public static Bitmap getCroppedBitmap(Bitmap bitmap, int radius) {
Bitmap _newBitmap;
if(bitmap.getWidth() != radius || bitmap.getHeight() != radius) {
_newBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false);
}
else{
_newBitmap = bitmap;
}
Bitmap _outBitmap = Bitmap.createBitmap(_newBitmap.getWidth(),
_newBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas _canvas = new Canvas(_outBitmap);
Paint _paint = new Paint();
Rect _rect = new Rect(0, 0, _newBitmap.getWidth(), _newBitmap.getHeight());
_paint.setAntiAlias(true);
_paint.setFilterBitmap(true);
_paint.setDither(true);
_canvas.drawARGB(0, 0, 0, 0);
_paint.setColor(Color.parseColor("#BAB399"));
_canvas.drawCircle(_newBitmap.getWidth() / 2, _newBitmap.getHeight() / 2,
_newBitmap.getWidth() / 2.5f, _paint);
_paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
_canvas.drawBitmap(_newBitmap, _rect, _rect, _paint);
return _outBitmap;
}
}
In the xml:
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/imgShot_shotDetail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
In the activity:
NetworkImageView imgShot = headerView.findViewById(R.id.imgShot_shotDetail);
imgShot.setImageUrl(url, AppControllerImage.getInstance().getImageLoader());
imgShot.setDefaultImageResId(R.drawable.notfound);
imgShot.setErrorImageResId(R.drawable.notfound);
In this case, I've used NetworkImageView
from Volley library, but you can use a ImageView
instead. You just have to adapt the code.