-
-
Save redleafar/c13d6783b90910c2932938034080bced to your computer and use it in GitHub Desktop.
Android Rounded Image
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
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.Bitmap.Config; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.PorterDuff.Mode; | |
import android.graphics.PorterDuffXfermode; | |
import android.graphics.Rect; | |
import android.graphics.drawable.BitmapDrawable; | |
import android.graphics.drawable.Drawable; | |
import android.support.v7.widget.AppCompatImageView; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
public class MLRoundedImageView extends AppCompatImageView { | |
public MLRoundedImageView(Context context) { | |
super(context); | |
} | |
public MLRoundedImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public MLRoundedImageView(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 b = ((BitmapDrawable) drawable).getBitmap(); | |
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); | |
int w = getWidth(), h = getHeight(); | |
Bitmap roundBitmap = getCroppedBitmap(bitmap, w, h); | |
canvas.drawBitmap(roundBitmap, 0, 0, null); | |
} | |
public static Bitmap getCroppedBitmap(Bitmap bmp, int wView, int hView) { | |
Bitmap aux; | |
Bitmap sbmp; | |
float wFactor; | |
float hFactor; | |
int diameter; | |
int radius; | |
if (bmp.getWidth() != wView || bmp.getHeight() != hView) { | |
float bmpWidht = bmp.getWidth(); | |
float bmpHeight = bmp.getHeight(); | |
wFactor = bmpWidht / wView; | |
hFactor = bmpHeight / hView; | |
float factor = Math.min(wFactor, hFactor); | |
float newWidht = bmpWidht/ factor; | |
float newHeight = bmpHeight / factor; | |
int newWidthInt = (int) newWidht; | |
int newHeightInt = (int) newHeight; | |
aux = Bitmap.createScaledBitmap(bmp, newWidthInt, newHeightInt, false); | |
int dx = Math.abs(aux.getWidth() - wView); | |
int dy = Math.abs(aux.getHeight() - hView); | |
//Log.d("dx", "dx, dy, wView, hView,aux.getWidth(), aux.getHeight(), bmp.getWidth(), bmp.getHeight():, "+String.valueOf(dx)+","+String.valueOf(dy) + "," + String.valueOf(wView)+ "," +String.valueOf(hView) + "," +String.valueOf(aux.getWidth())+ "," +String.valueOf(aux.getHeight())+ "," +String.valueOf(bmp.getWidth())+ "," +String.valueOf(bmp.getHeight())); | |
int deltaX = wView + dx/2; | |
int deltaY = hView + dy/2; | |
if (deltaX > aux.getWidth()){ | |
deltaX = aux.getWidth(); | |
} | |
if (deltaY > aux.getHeight()){ | |
deltaY = aux.getHeight(); | |
} | |
sbmp = Bitmap.createBitmap(aux, dx/2, dy/2 , deltaX, deltaY); | |
} else { | |
sbmp = bmp; | |
} | |
diameter = Math.min(sbmp.getWidth(),sbmp.getHeight()); | |
radius = diameter /2; | |
Bitmap output = Bitmap.createBitmap(diameter, diameter, | |
Config.ARGB_8888); | |
Canvas canvas = new Canvas(output); | |
final int color = 0xffa19774; | |
final Paint paint = new Paint(); | |
final Rect rect = new Rect(0, 0, diameter, diameter); | |
paint.setAntiAlias(true); | |
paint.setFilterBitmap(true); | |
paint.setDither(true); | |
canvas.drawARGB(0, 0, 0, 0); | |
paint.setColor(Color.parseColor("#BAB399")); | |
canvas.drawCircle(wView / 2 , | |
hView / 2 , radius , paint); | |
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); | |
canvas.drawBitmap(sbmp, rect, rect, paint); | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment