效果图:
一个边框,角落附带两条斜线,形成了类似贴纸的效果。
完整代码如下:
import android.content.Context;
import android.content.res.Resources.Theme;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Build.VERSION_CODES;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.StyleRes;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.RelativeLayout;
/**
* 右下角有两个短斜线,类似便签
*
* @author lu
* @date 2017/8/22 17:55
*/
public class MomoLayout extends RelativeLayout {
private Paint paint;
public MomoLayout(@NonNull Context context) {
this(context, null);
}
public MomoLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MomoLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@RequiresApi(api = VERSION_CODES.LOLLIPOP)
public MomoLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
setWillNotDraw(false);//开启onDraw()方法。背景为空时,onDraw(),被优化掉,将不调用
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(2);
TypedValue typedValue = new TypedValue();
Theme theme = getContext().getTheme();
boolean found = theme.resolveAttribute(android.R.attr.colorPrimary, typedValue, true);
if (!found) {
found = theme.resolveAttribute(android.R.attr.colorPrimaryDark, typedValue, true);
if (!found) {
theme.resolveAttribute(android.R.attr.colorAccent, typedValue, true);
}
}
int id = typedValue.resourceId;
if (id > 0) {
int color = getResources().getColor(id);
paint.setColor(color);
} else {
paint.setColor(0x55000000);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawSlash(canvas);
drawMarginLine(canvas);
}
/**
* 绘制外边框
*
* @param canvas
*/
private void drawMarginLine(Canvas canvas) {
paint.setStyle(Style.STROKE);
float strokeWidth = paint.getStrokeWidth();
float offset = strokeWidth / 2f;
if (getWidth() > offset && getHeight() > offset) {
canvas.drawRect(0 + offset, 0 + offset, getWidth() - offset, getHeight() - offset, paint);
}
}
/**
* 绘制斜线
*
* @param canvas
*/
private void drawSlash(Canvas canvas) {
paint.setStyle(Style.FILL);
float limitW = getWidth() - 10f;
float limitH = getHeight() - 10f;
if (limitH > 0 && limitW > 0) {
float startX1 = limitW - limitW / 25f;
float startY1 = limitH;
float stopX1 = limitW;
float stopY1 = limitH - limitH / 25f;
canvas.drawLine(startX1, startY1, stopX1, stopY1, paint);
float startX2 = limitW - limitW / 50f;
float startY2 = limitH;
float stopX2 = limitW;
float stopY2 = limitH - limitH / 50f;
canvas.drawLine(startX2, startY2, stopX2, stopY2, paint);
}
}
}
其他说明:
- 一般用于包裹View;
- 为方便布局,继承自RelativeLayout
- 没有自定义属性
- 使用方式和普通的 RelativeLayout 一样
- 描边方式为内描边,线宽2px
——end