Created
September 4, 2014 15:45
-
-
Save richardleggett/2f38f2c9c431326ee5e9 to your computer and use it in GitHub Desktop.
RoundedFrameLayout - FrameLayout clipped by a rounded rectangle
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.example.ui.widget; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.Path; | |
import android.graphics.RectF; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.FrameLayout; | |
import ccom.example.R; | |
/** | |
* A not-so-efficient rounded-rectangle-clipped FrameLayout, until Android-L's clipping Outlines are | |
* wideley available. | |
* | |
* NOTE: This doesn't support anti-aliasing, if you just need a rounded ImageView use the more | |
* efficient BitmapDrawable method which does: http://evel.io/2013/07/21/rounded-avatars-in-android/ | |
* | |
* In my case the rounded corner was to mask an image as you scrolled them in a ViewPager, so by | |
* combining this with a RoundedImageView it appears anti-aliased unless actively scrolling. | |
* | |
* Created by richardleggett on 04/09/2014. | |
*/ | |
public class RoundedFrameLayout extends FrameLayout { | |
private float mCornerRadius = 12; | |
private Path mPath; | |
private RectF mRect; | |
public RoundedFrameLayout(Context context) { | |
super(context, null, 0); | |
initView(null); | |
} | |
public RoundedFrameLayout(Context context, AttributeSet attrs) { | |
super(context, attrs, 0); | |
initView(attrs); | |
} | |
public RoundedFrameLayout(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
initView(attrs); | |
} | |
private void initView(AttributeSet attributes) { | |
if(attributes != null) { | |
TypedArray array = getContext().obtainStyledAttributes(attributes, | |
R.styleable.RoundedFrameLayout); | |
if (array != null) { | |
mCornerRadius = array.getDimension(R.styleable.RoundedFrameLayout_corner_radius, 0); | |
array.recycle(); | |
} | |
} | |
mPath = new Path(); | |
mRect = new RectF(); | |
setWillNotDraw(false); | |
} | |
public void setCornerRadius(float cornerRadius) { | |
mCornerRadius = cornerRadius; | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
mPath.reset(); | |
mRect.set(0, 0, w, h); | |
mPath.addRoundRect(mRect, mCornerRadius, mCornerRadius, Path.Direction.CCW); | |
mPath.close(); | |
} | |
@Override | |
protected boolean drawChild(Canvas canvas, View child, long drawingTime) { | |
int count = canvas.save(); | |
canvas.clipPath(mPath); | |
boolean result = super.drawChild(canvas, child, drawingTime); | |
canvas.restoreToCount(count); | |
return result; | |
} | |
} |
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
<declare-styleable name="RoundedFrameLayout"> | |
<attr name="corner_radius" format="dimension" /> | |
</declare-styleable> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment