package com.example.hehe.myapplication;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
/**
* Created by hehe on 2017/8/4.
*/
public class BorderTextView extends android.support.v7.widget.AppCompatTextView {
public static final float DEFAULT_STROKE_WIDTH = 1.0f; // 默认边框宽度, 1dp
public static final float DEFAULT_CORNER_RADIUS = 2.0f; // 默认圆角半径, 2dp
public static final float DEFAULT_LR_PADDING = 6f; // 默认左右内边距
public static final float DEFAULT_TB_PADDING = 2f; // 默认上下内边距
private int strokeWidth; // 边框线宽
private int strokeColor; // 边框颜色
private int cornerRadius; // 圆角半径
private int backgroundColor; // 背景颜色
private boolean mFollowTextColor; // 边框颜色是否跟随文字颜色
private Paint paint;
private RectF rectF;
public BorderTextView(Context context) {
this(context, null);
}
public BorderTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public BorderTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
strokeWidth = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_STROKE_WIDTH, displayMetrics);
cornerRadius = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_CORNER_RADIUS, displayMetrics);
//读取属性值
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BorderTextView);
strokeWidth = typedArray.getDimensionPixelSize(R.styleable.BorderTextView_strokeWidth, strokeWidth);
cornerRadius = typedArray.getDimensionPixelSize(R.styleable.BorderTextView_cornerRadius, cornerRadius);
strokeColor = typedArray.getColor(R.styleable.BorderTextView_strokeColor, Color.TRANSPARENT);
backgroundColor = typedArray.getColor(R.styleable.BorderTextView_backgroundColor, Color.TRANSPARENT);
mFollowTextColor = typedArray.getBoolean(R.styleable.BorderTextView_followTextColor, true);
// 如果使用时没有设置内边距, 设置默认边距
int paddingLeft = getPaddingLeft() == 0 ? (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_LR_PADDING, displayMetrics) : getPaddingLeft();
int paddingRight = getPaddingRight() == 0 ? (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_LR_PADDING,
displayMetrics) : getPaddingRight();
int paddingTop = getPaddingTop() == 0 ? (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_TB_PADDING, displayMetrics) : getPaddingTop();
int paddingBottom = getPaddingBottom() == 0 ? (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_TB_PADDING,
displayMetrics) : getPaddingBottom();
setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
paint = new Paint();
paint.setStyle(Paint.Style.STROKE); //描边
paint.setAntiAlias(true);// 设置画笔为无锯齿
paint.setStrokeWidth(strokeWidth);// 线宽
// 设置边框线的颜色, 如果声明为边框跟随文字颜色且当前边框颜色与文字颜色不同时重新设置边框颜色
if (mFollowTextColor && strokeColor != getCurrentTextColor()) {
strokeColor = getCurrentTextColor();
}
paint.setColor(strokeColor);
// 画圆角矩形
rectF.left = rectF.top = 0.5f * strokeWidth;
rectF.right = getMeasuredWidth() - strokeWidth;
rectF.bottom = getMeasuredHeight() - strokeWidth;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);
}
public void setBackgroundColorDraw(int color) {
paint = null;
paint = new Paint();
paint.setStyle(Paint.Style.FILL_AND_STROKE); //描边+背景
paint.setAntiAlias(true);// 设置画笔为无锯齿
paint.setStrokeWidth(strokeWidth);// 线宽
// 设置边框线的颜色, 如果声明为边框跟随文字颜色且当前边框颜色与文字颜色不同时重新设置边框颜色
if (mFollowTextColor && strokeColor != getCurrentTextColor()) {
strokeColor = getCurrentTextColor();
}
paint.setColor(strokeColor);
// 画圆角矩形
rectF.left = rectF.top = 0.5f * strokeWidth;
rectF.right = getMeasuredWidth() - strokeWidth;
rectF.bottom = getMeasuredHeight() - strokeWidth;
invalidate();
}
public void setBorderColorDraw(int color) {
paint = null;
paint = new Paint();
paint.setStyle(Paint.Style.STROKE); //描边
paint.setAntiAlias(true);// 设置画笔为无锯齿
paint.setStrokeWidth(strokeWidth);// 线宽
// 设置边框线的颜色, 如果声明为边框跟随文字颜色且当前边框颜色与文字颜色不同时重新设置边框颜色
if (mFollowTextColor && strokeColor != getCurrentTextColor()) {
strokeColor = getCurrentTextColor();
}
paint.setColor(strokeColor);
// 画圆角矩形
rectF.left = rectF.top = 0.5f * strokeWidth;
rectF.right = getMeasuredWidth() - strokeWidth;
rectF.bottom = getMeasuredHeight() - strokeWidth;
invalidate();
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="BorderTextView">
<attr name="strokeWidth" format="dimension"/>
<attr name="cornerRadius" format="dimension"/>
<attr name="strokeColor" format="dimension"/>
<attr name="backgroundColor" format="dimension"/>
<attr name="followTextColor" format="boolean"/>
</declare-styleable>
</resources>