Skip to content

Instantly share code, notes, and snippets.

@oksep
Last active February 19, 2016 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oksep/876964c99864a5fc2383 to your computer and use it in GitHub Desktop.
Save oksep/876964c99864a5fc2383 to your computer and use it in GitHub Desktop.
RoundedCorner
public class RoundedCornerLayout extends FrameLayout {
private final static float CORNER_RADIUS = 6.0f;
private float cornerRadius;
public RoundedCornerLayout(Context context) {
super(context);
init(context, null, 0);
}
public RoundedCornerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
@Override
protected void dispatchDraw(Canvas canvas) {
int count = canvas.save();
final Path path = new Path();
path.addRoundRect(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), cornerRadius, cornerRadius, Path.Direction.CW);
canvas.clipPath(path, Region.Op.REPLACE);
canvas.clipPath(path);
super.dispatchDraw(canvas);
canvas.restoreToCount(count);
}
}
public class RoundCornerView extends View{
public RoundCornerView(Context context) {
super(context);
}
public RoundCornerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundCornerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
protected void onDraw(android.graphics.Canvas canvas)
{
Paint paint = new Paint();
paint.setAlpha(255);
canvas.translate(0, 30);
paint.setColor(Color.BLUE);
Path mPath = new Path();
mPath.addRoundRect(new RectF(0, 0, 100,100),20,20, Path.Direction.CCW);
canvas.clipPath(mPath, Region.Op.INTERSECT);
paint.setColor(Color.GREEN);
paint.setAntiAlias(true);
canvas.drawRect(0, 0, 120,120,paint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment