Skip to content

Instantly share code, notes, and snippets.

@pcevikogullari
Created March 4, 2016 12:13
Show Gist options
  • Save pcevikogullari/8adcff53f5ba23e75d19 to your computer and use it in GitHub Desktop.
Save pcevikogullari/8adcff53f5ba23e75d19 to your computer and use it in GitHub Desktop.
public class RoundedLinearLayout extends LinearLayout {
String TAG = getClass().getSimpleName();
Path mPath;
float mCornerRadius;
public boolean isCornerSet = false;
public RoundedLinearLayout(Context context) {
super(context);
}
public RoundedLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundedLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void draw(Canvas canvas) {
RectF r = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
mPath = new Path();
if(!isCornerSet){
mCornerRadius = canvas.getHeight()/2;
isCornerSet = true;
}
mPath.addRoundRect(r, mCornerRadius, mCornerRadius, Path.Direction.CW);
mPath.close();
canvas.save();
canvas.clipPath(mPath);
super.draw(canvas);
canvas.restore();
Log.v(TAG, "onDraw");
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// setPath(w, h);
Log.v(TAG, "onSizeChanged");
}
public void setPath(int w, int h){
RectF r = new RectF(0, 0, w, h);
mCornerRadius = h/2;
mPath = new Path();
mPath.addRoundRect(r, mCornerRadius, mCornerRadius, Path.Direction.CW);
mPath.close();
}
public void setCornerRadius(int radius) {
mCornerRadius = radius;
invalidate();
Log.v(TAG, "setCornedRadius");
}
public float getCornerRadius(){
return mCornerRadius;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment