Skip to content

Instantly share code, notes, and snippets.

@kamikat
Last active August 29, 2015 14:06
Show Gist options
  • Save kamikat/fcb609ac996b68be1510 to your computer and use it in GitHub Desktop.
Save kamikat/fcb609ac996b68be1510 to your computer and use it in GitHub Desktop.
Evil implementation to rounded corner containers.
<resources>
<declare-styleable name="MaskView">
<attr name="maskColor" format="color" />
<attr name="borderColor" format="color" />
<attr name="cornerRadius" format="dimension" />
<attr name="maskShape" format="enum">
<enum name="rounded_rectangle" value="0" />
<enum name="oval" value="1" />
</attr>
<attr name="borderWidth" format="dimension" />
</declare-styleable>
</resources>
package com.tietie.foundation.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import com.tietie.foundation.R;
public class MaskView extends View {
public static final int SHAPE_ROUNDED_RECTANGLE = 0;
public static final int SHAPE_OVAL = 1;
private int mMaskColor;
private int mBorderColor;
private int mBorderWidth;
private int mCornerRadius;
private int mMaskShape;
private RectF mDrawingRect;
private Paint mPaintEraser;
private Paint mPaintBorder;
private Bitmap mDrawingCache;
private Canvas mDrawingCanvas;
public MaskView(Context context) {
super(context);
}
public MaskView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MaskView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.MaskView,
0, 0);
try {
mMaskShape = a.getInt(R.styleable.MaskView_maskShape, SHAPE_ROUNDED_RECTANGLE);
mMaskColor = a.getColor(R.styleable.MaskView_maskColor, Color.WHITE);
mBorderColor = a.getColor(R.styleable.MaskView_borderColor, Color.GRAY);
mBorderWidth = a.getDimensionPixelSize(R.styleable.MaskView_borderWidth, 0);
mCornerRadius = a.getDimensionPixelSize(R.styleable.MaskView_cornerRadius, 0);
} finally {
a.recycle();
}
mPaintEraser = new Paint() {{
this.setAntiAlias(true);
this.setColor(Color.WHITE);
this.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}};
mPaintBorder = new Paint() {{
this.setColor(mBorderColor);
setStrokeWidth(mBorderWidth);
this.setStyle(Style.STROKE);
}};
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mDrawingRect = new RectF(getPaddingLeft(), getPaddingTop(),
w - getPaddingLeft() - getPaddingRight(),
h - getPaddingTop() - getPaddingBottom());
if (mDrawingCache != null) {
mDrawingCache.recycle();
}
mDrawingCache = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mDrawingCanvas = new Canvas(mDrawingCache);
mDrawingCache.eraseColor(mMaskColor);
switch (mMaskShape) {
case SHAPE_ROUNDED_RECTANGLE:
mDrawingCanvas.drawRoundRect(mDrawingRect, mCornerRadius, mCornerRadius, mPaintEraser);
if (mBorderWidth > 0) {
mDrawingCanvas.drawRoundRect(mDrawingRect, mCornerRadius, mCornerRadius, mPaintBorder);
}
break;
case SHAPE_OVAL:
mDrawingCanvas.drawOval(mDrawingRect, mPaintEraser);
if (mBorderWidth > 0) {
mDrawingCanvas.drawOval(mDrawingRect, mPaintBorder);
}
break;
}
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mDrawingCache, 0, 0, null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment