效果见下图:
package com.logistics.widgets;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.usung.gxzy_logistics.R;
/**
* Descriptions:颜色方框和文字,用于统计图颜色标注
* Created by fenghui on 2018/9/25.
*/
public class CustomStatisticsColor extends View {
private int width; // 整个控件的宽
private int height; // 整个控件的高
private float tagWidth = dpToPx(15); // tag宽
private float tagHeight = dpToPx(15); // tag高
private float offsetSpacing = dpToPx(5); // 文字和前部分标记之间的间距
private Paint paint;
private Paint paintText;
private int tagColor = 0xff315178; // 标记颜色(前半部分颜色)
private int textColor = 0xff00ffcd; // 标记颜色(前半部分颜色)
private RectF rectF; // 矩形 用于绘制矩形
private Rect rect; // 用于测量文字
private String TAG = "fenghui";
private String text = "颜色";
public CustomStatisticsColor(Context context) {
super(context);
init();
}
public CustomStatisticsColor(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomStatisticsColor);
tagColor = typedArray.getColor(R.styleable.CustomStatisticsColor_tagColor, tagColor);
textColor = typedArray.getColor(R.styleable.CustomStatisticsColor_tagTextColor, getResources().getColor(R.color.blue));
text = typedArray.getString(R.styleable.CustomStatisticsColor_tagText);
tagWidth = typedArray.getDimensionPixelOffset(R.styleable.CustomStatisticsColor_tagWidth, 15);
tagHeight = typedArray.getDimensionPixelOffset(R.styleable.CustomStatisticsColor_tagHeight, 15);
offsetSpacing = typedArray.getDimensionPixelOffset(R.styleable.CustomStatisticsColor_offsetSpacing, 5);
typedArray.recycle();
init();
}
void init(){
paint = new Paint();
paint.setColor(tagColor);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paintText = new Paint();
paintText.setColor(textColor);
paintText.setStyle(Paint.Style.FILL);
paintText.setTextSize(spToPx(12));
paintText.setAntiAlias(true);
rect = new Rect();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 获取
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight= MeasureSpec.getSize(heightMeasureSpec);
int mode = MeasureSpec.getMode(widthMeasureSpec);
paintText.getTextBounds(text, 0, text.length(), rect);
if (mode == MeasureSpec.EXACTLY) {
width = sizeWidth;
height = sizeHeight;
} else {
width = (int) (tagWidth + offsetSpacing + rect.width() + rect.right);
if (tagHeight > rect.height()){
height = (int) tagHeight;
}else{
height = rect.height();
}
}
Log.i(TAG, "width========= " + width + "\n" + "height========= " + height);
setMeasuredDimension(width, height);
rectF = new RectF(0, 0, tagWidth, tagHeight);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(rectF, paint);
// canvas.drawText(text,offsetSpacing + rectF.width(), tagHeight/2 + rect.height()/2 -rect.bottom, paintText);
canvas.drawText(text,tagWidth + offsetSpacing, tagHeight/2 + rect.height()/2 -rect.bottom, paintText);
}
/**
* dp转化成为px
*
* @param dp
* @return
*/
private int dpToPx(float dp) {
float density = getContext().getResources().getDisplayMetrics().density;
return (int) (dp * density + 0.5f * (dp >= 0 ? 1 : -1));
}
/**
* sp转化为px
*
* @param sp
* @return
*/
private int spToPx(int sp) {
float scaledDensity = getContext().getResources().getDisplayMetrics().scaledDensity;
return (int) (scaledDensity * sp + 0.5f * (sp >= 0 ? 1 : -1));
}
}