package com.yelp.android.ui.widgets; | |
import com.yelp.android.R; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.PorterDuff; | |
import android.graphics.PorterDuffXfermode; | |
import android.graphics.Rect; | |
import android.graphics.RectF; | |
import android.graphics.Xfermode; | |
import android.graphics.drawable.BitmapDrawable; | |
import android.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
/** | |
* An ImageView that allows a pixel corner radius | |
* to be specified. The image's corners will be rounded | |
* to this radius, with transparent behind the image. | |
* | |
* <b>WARNING:</b> This is only supported for bitmap | |
* image sources. If other types of drawable are set | |
* as the image source, the corners <b>will not be | |
* rounded.</b> | |
* @author alex | |
* | |
*/ | |
public class RoundedImageView extends ImageView { | |
private float mCornerRadius; | |
public RoundedImageView(Context context) { | |
super(context); | |
} | |
public RoundedImageView(Context context, AttributeSet attributes) { | |
super(context, attributes); | |
TypedArray array = context.obtainStyledAttributes(attributes, R.styleable.RoundedImageView); | |
if (array != null) { | |
mCornerRadius = array.getDimension(R.styleable.RoundedImageView_corner_radius, 0); | |
array.recycle(); | |
} | |
} | |
/** | |
* Sets the corner radius for rounded image corners in | |
* absolutely display pixels. | |
* @param cornerRadius | |
*/ | |
public void setCornerRadius(float cornerRadius) { | |
mCornerRadius = cornerRadius; | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
// Round some corners betch! | |
Drawable maiDrawable = getDrawable(); | |
if (maiDrawable instanceof BitmapDrawable && mCornerRadius > 0) { | |
Paint paint = ((BitmapDrawable) maiDrawable).getPaint(); | |
final int color = 0xff000000; | |
Rect bitmapBounds = maiDrawable.getBounds(); | |
final RectF rectF = new RectF(bitmapBounds); | |
// Create an off-screen bitmap to the PorterDuff alpha blending to work right | |
int saveCount = canvas.saveLayer(rectF, null, | |
Canvas.MATRIX_SAVE_FLAG | | |
Canvas.CLIP_SAVE_FLAG | | |
Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | | |
Canvas.FULL_COLOR_LAYER_SAVE_FLAG | | |
Canvas.CLIP_TO_LAYER_SAVE_FLAG); | |
// Resize the rounded rect we'll clip by this view's current bounds | |
// (super.onDraw() will do something similar with the drawable to draw) | |
getImageMatrix().mapRect(rectF); | |
paint.setAntiAlias(true); | |
canvas.drawARGB(0, 0, 0, 0); | |
paint.setColor(color); | |
canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint); | |
Xfermode oldMode = paint.getXfermode(); | |
// This is the paint already associated with the BitmapDrawable that super draws | |
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); | |
super.onDraw(canvas); | |
paint.setXfermode(oldMode); | |
canvas.restoreToCount(saveCount); | |
} else { | |
super.onDraw(canvas); | |
} | |
} | |
} |
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<!-- Attributes for RoundedImageView --> | |
<declare-styleable name="RoundedImageView"> | |
<!-- Size to round image corners by. Default is zero, unrounded. | |
<b>NOTE:</b> This only works for bitmap image sources. --> | |
<attr name="corner_radius" format="dimension" /> | |
</declare-styleable> | |
</resources> |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
beanmilk
commented
Jul 3, 2014
Can I use this class for my commercial application? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
Sottti
Jul 19, 2014
Hi there,
Thanks for sharing this, I found it very interesting.
Just a question, how this does compare with the Romain Guy´s solution?
http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/
Thanks!!
Sottti
commented
Jul 19, 2014
Hi there, Thanks for sharing this, I found it very interesting. Just a question, how this does compare with the Romain Guy´s solution? Thanks!! |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
kingofkhan08
commented
Jul 23, 2014
in listview taking some time to come into round rect shape |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can I use this class for my commercial application?
Can you tell me which license is used for this code?
Thanks in advance. ;)
Happy coding!