Created
January 10, 2012 02:47
-
-
Save Pretz/1586565 to your computer and use it in GitHub Desktop.
Andround Rounded Image View
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
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); | |
} | |
} | |
} |
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
<?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> |
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!!
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!