-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Great Man, It worked perfectly with RecyclerView. Can you please provide with the rounded corners imageView also?