Skip to content

Instantly share code, notes, and snippets.

@guilhermearaujo
Created April 3, 2014 14:30
Show Gist options
  • Save guilhermearaujo/9955426 to your computer and use it in GitHub Desktop.
Save guilhermearaujo/9955426 to your computer and use it in GitHub Desktop.
RoundedNetworkImageView
//
// RoundedNetworkImageView
//
// Created by Guilherme Araújo on 4/3/14.
//
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import com.android.volley.toolbox.NetworkImageView;
import org.jetbrains.annotations.NotNull;
public class RoundedNetworkImageView extends NetworkImageView {
private int borderColor = R.color.black;
private int borderWidth = 5;
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);
}
public void setBorderColor(int resourceID) {
borderColor = resourceID;
}
public void setBorderWidth(int border) {
borderWidth = border;
}
public int getBorderWidth() {
return borderWidth;
}
@Override
protected void onDraw(@NotNull Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null || getWidth() == 0 || getHeight() == 0)
return;
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
if (b != null) {
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
int radius = (getWidth() < getHeight()) ? getWidth() / 2 : getHeight() / 2;
Bitmap roundBitmap = getCroppedBitmap(bitmap, radius);
canvas.drawBitmap(roundBitmap, 0, 0, null);
}
}
public Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap scaledBmp = Bitmap.createScaledBitmap(bmp, radius * 2, radius * 2, false);
Bitmap output = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
// Draws a circle to create the border
paint.setColor(borderColor);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);
// Draws the image subtracting the border width
BitmapShader s = new BitmapShader(scaledBmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(s);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius - borderWidth - 0.5f, paint);
return output;
}
}
@AssafMashiah
Copy link

My images are coming out with a blue filter over them for some reason...

@rvacoder
Copy link

rvacoder commented Oct 2, 2014

The same for me, I got a blue image.

@pero86
Copy link

pero86 commented Oct 5, 2015

I just removed lines 80 and 81 and it works perfectly, I don't need border anyway.

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