import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;


public class RoundRectLayout extends RelativeLayout {
private Path mPath;
private int mRadius;

private int mWidth;
private int mHeight;
private int mLastRadius;

public static final int MODE_NONE = 0;
public static final int MODE_ALL = 1;
public static final int MODE_LEFT = 2;
public static final int MODE_TOP = 3;
public static final int MODE_RIGHT = 4;
public static final int MODE_BOTTOM = 5;

private int mRoundMode = MODE_ALL;
private Paint mPaint;

public RoundRectLayout(Context context) {
super(context);
init();
}

public RoundRectLayout(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init();
}

public RoundRectLayout(Context context, AttributeSet attributeSet, int i) {
super(context, attributeSet, i);
init();
}

private void init(){
setBackgroundDrawable(new ColorDrawable(0x00000000));
mPath = new Path();
mPath.setFillType(Path.FillType.EVEN_ODD);
setCornerRadius(15);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
// mPaint.setStrokeWidth(4);
}

/**
* @param roundMode
*/
public void setRoundMode(int roundMode){
mRoundMode = roundMode;
}

/**
* @param radius
*/
public void setCornerRadius(int radius){
mRadius = radius;
}

private void checkPathChanged(){
if(getWidth() == mWidth && getHeight() == mHeight && mLastRadius == mRadius){
return;
}
mWidth = getWidth();
mHeight = getHeight();
mLastRadius = mRadius;
mPath.reset();
switch (mRoundMode){
case MODE_ALL:
mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight), mRadius, mRadius, Path.Direction.CW);
break;
case MODE_LEFT:
mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight),
new float[]{mRadius, mRadius, 0, 0, 0, 0, mRadius, mRadius},
Path.Direction.CW);
break;
case MODE_TOP:
mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight),
new float[]{mRadius, mRadius, mRadius, mRadius, 0, 0, 0, 0},
Path.Direction.CW);
break;
case MODE_RIGHT:
mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight),
new float[]{0, 0, mRadius, mRadius, mRadius, mRadius, 0, 0},
Path.Direction.CW);
break;
case MODE_BOTTOM:
mPath.addRoundRect(new RectF(0, 0, mWidth, mHeight),
new float[]{0, 0, 0, 0, mRadius, mRadius, mRadius, mRadius},
Path.Direction.CW);
break;
}
}

@Override
public void draw(Canvas canvas) {
if(mRoundMode != MODE_NONE){
// RectF mRectF = new RectF(0,0, mWidth,mHeight);
int saveCount = canvas.save();
checkPathChanged();
// mPaint.setColor(getContext().getColor(R.color.launcher_navi_widget_stroke_color));
// canvas.drawRoundRect(mRectF,16,16,mPaint);
canvas.clipPath(mPath);
super.draw(canvas);
canvas.restoreToCount(saveCount);
}else {
super.draw(canvas);
}
}
}