Last active
November 5, 2015 11:28
-
-
Save rghwang/13db490a07112cc99fa1 to your computer and use it in GitHub Desktop.
CentorCrop + CircularNetworkImageView
This file contains hidden or 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
package com.wantedlab.android.wanted.helpers; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.Bitmap.Config; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.PorterDuff.Mode; | |
import android.graphics.PorterDuffXfermode; | |
import android.graphics.Rect; | |
import android.graphics.RectF; | |
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 width = bitmap.getWidth(); | |
int offsetX = 0; | |
int offsetY = 0; | |
if(bitmap.getWidth()>bitmap.getHeight()){ | |
// height가 더 적을 때. 가로로 넓은 경우 | |
offsetX = (width - bitmap.getHeight())/2; | |
width = bitmap.getHeight(); | |
}else{ | |
offsetY = (bitmap.getHeight() - width)/2; | |
} | |
Bitmap output = Bitmap.createBitmap(width, | |
width, Config.ARGB_8888); | |
Canvas canvas = new Canvas(output); | |
final int color = 0xff424242; | |
final Paint paint = new Paint(); | |
final Rect rect = new Rect(offsetX, offsetY, offsetX+ width, offsetY+ width); | |
final RectF rectF = new RectF(rect); | |
final float roundPx = width / 2; | |
paint.setAntiAlias(true); | |
canvas.drawARGB(0, 0, 0, 0); | |
canvas.save(); | |
canvas.translate(-offsetX, -offsetY); | |
paint.setColor(color); | |
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); | |
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); | |
canvas.drawBitmap(bitmap, rect, rect, paint); | |
canvas.restore(); | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CircularNetworkImage https://gist.github.com/bkurzius/99c945bd1bdcf6af8f99