Created
April 25, 2014 08:18
-
-
Save kishu27/11281843 to your computer and use it in GitHub Desktop.
Just a quick and dirty method to get a rounded QuickContactBadge
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
/** | |
* A rounded version of {@link QuickContactBadge] | |
* @author kishu27 (http://linkd.in/1laN852) | |
* | |
*/ | |
public class RoundedQuickContactBadge extends QuickContactBadge { | |
/** | |
* This path is used to mask out the outer edges of a circle on this View | |
*/ | |
Path clipPath; | |
public RoundedQuickContactBadge(Context context) { | |
super(context); | |
init(); //Set our initialization | |
} | |
public RoundedQuickContactBadge(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); //Set our initialization | |
} | |
public RoundedQuickContactBadge(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(); //Set our initialization | |
} | |
/** | |
* Initialize our stuff | |
*/ | |
private void init() { | |
//Use reflection to reset the default triangular overlay from default quick contact badge | |
try { | |
Field field = QuickContactBadge.class.getDeclaredField("mOverlay"); | |
field.setAccessible(true); | |
//Using a drawable that draws a white circle. This could be set as null to not draw anything at all | |
field.set(this, getResources().getDrawable(R.drawable.ic_quick_contact_overlay)); | |
} catch (Exception e) { | |
//No-op, just well off with the default overlay | |
} | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
/* | |
* Create a new clip path. Anything outside this path will be clipped from this view and not drawn by onDraw method | |
*/ | |
clipPath = new Path(); | |
//Adding a circle. The circle will be positioned in the center using x = w/2 and y = w/2 | |
//Circle will be limiting it's radius to the smaller one of height or width. | |
//Direction doesn't matter | |
clipPath.addCircle(w/2, h/2, w < h? w/2 : h/2, Direction.CW); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
//Erase everything out of our little circle in clipPath and hence create the real rounded QuickContactBadge | |
canvas.clipPath(clipPath); | |
//Do everything else that original badge does. Drawing of the overlay is also handled there | |
super.onDraw(canvas); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment