一个colortagview,告别繁琐的xml

来源:互联网 发布:日发期货 知乎 编辑:程序博客网 时间:2024/05/22 09:49

看一眼上次写博客的时间,俨然已经过去了好久,必须要写写什么了,因为代码才能让人觉得内心的平静。。。OK,废话不多说,在项目中有时候可能遇到下面的效果如图
这里写图片描述
一般人一看这个简单只需要配置drawable下的xml不就完了么,但是如果你需要加载多个且每个的颜色还都不一样,你还是要写xml么,显然是不太合适的。要实现这样的就是给个自定义view添加背景同时确定绘制的view在其中心就OK了
so.看具体的代码

package com.vc.view.colortagview;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.util.AttributeSet;import android.util.TypedValue;import android.view.View;/** * Created by vc on 2017/7/5. */public class ColorTagView extends View {    private int width;    private int height;    private int mTagRadius;    private int mTagBgColor;    private String mTagText;    private int mTagTextSize;    private int mTagTextColor;    public static final int FILL =0;    public static final int STROKE= 1;    private int mTagBgStyle;    private int mTagBgStrokeWidth;    RectF rectF;    Paint  paint  =new Paint(Paint.ANTI_ALIAS_FLAG);    private Paint mTextPaint =new Paint(Paint.ANTI_ALIAS_FLAG);;    public ColorTagView(Context context) {        this(context,null);    }    public ColorTagView(Context context,  AttributeSet attrs) {        this(context, attrs,0);    }    public ColorTagView(Context context,  AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.ColorTagView);        mTagRadius = array.getDimensionPixelSize(R.styleable.ColorTagView_tag_radius,dp2px(20));        mTagBgColor = array.getColor(R.styleable.ColorTagView_tag_bg,Color.WHITE);        mTagBgStyle = array.getInt(R.styleable.ColorTagView_tag_bg_style,FILL);        mTagBgStrokeWidth  =array.getDimensionPixelSize(R.styleable.ColorTagView_tag_bg_stroke_width,dp2px(1));        mTagText  = array.getString(R.styleable.ColorTagView_tag_text);        mTagTextSize = array.getDimensionPixelSize(R.styleable.ColorTagView_tag_textSize,sp2px(14));        mTagTextColor = array.getColor(R.styleable.ColorTagView_tag_textColor,Color.BLACK);        array.recycle();        mTextPaint.setTextSize(mTagTextSize);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int widthMode = MeasureSpec.getMode(widthMeasureSpec);        int heightMode= MeasureSpec.getMode(heightMeasureSpec);        if(widthMode ==MeasureSpec.EXACTLY ){              width = MeasureSpec.getSize(widthMeasureSpec);        }else if(widthMode ==MeasureSpec.AT_MOST || widthMode ==MeasureSpec.UNSPECIFIED) {              width = (int) mTextPaint.measureText(mTagText) + getPaddingLeft() +getPaddingRight();        }        if(heightMode ==MeasureSpec.EXACTLY){            height = MeasureSpec.getSize(heightMeasureSpec);        }else  if(heightMode ==MeasureSpec.AT_MOST || heightMode ==MeasureSpec.UNSPECIFIED) {            Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();            height  = (int) (fontMetrics.bottom - fontMetrics.top) + getPaddingTop() + getPaddingBottom();        }        setMeasuredDimension(width,height);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        paint.setColor(mTagBgColor);        if(mTagBgStyle == FILL){            paint.setStyle(Paint.Style.FILL);        }else {            paint.setStyle(Paint.Style.STROKE);            paint.setStrokeWidth(mTagBgStrokeWidth);        }        if(mTagBgStyle == STROKE){            rectF =new RectF(mTagBgStrokeWidth,mTagBgStrokeWidth,getMeasuredWidth() -mTagBgStrokeWidth,getMeasuredHeight()-mTagBgStrokeWidth );            canvas.drawRoundRect(rectF,mTagRadius,mTagRadius,paint);        }else {            rectF =new RectF(0,0,getMeasuredWidth(),getMeasuredHeight());            canvas.drawRoundRect(rectF,mTagRadius,mTagRadius,paint);        }        mTextPaint.setColor(mTagTextColor);        // 计算绘制文本的高度        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();        int textHeight = (int) (getMeasuredHeight() / 2 - fontMetrics.descent + (fontMetrics.bottom - fontMetrics.top) / 2);        canvas.drawText(mTagText,getPaddingLeft(),textHeight,mTextPaint);    }    public int getTagRadius() {        return mTagRadius;    }    public void setTagRadius(int tagRadius) {        mTagRadius = dp2px(tagRadius);    }    public int getTagBgColor() {        return mTagBgColor;    }    public void setTagBgColor(int tagBgColor) {        mTagBgColor = tagBgColor;    }    public String getTagText() {        return mTagText;    }    public void setTagText(String tagText) {        mTagText = tagText;    }    public int getTagTextSize() {        return mTagTextSize;    }    public void setTagTextSize(int tagTextSize) {        mTagTextSize = tagTextSize;    }    public int getTagTextColor() {        return mTagTextColor;    }    public void setTagTextColor(int tagTextColor) {        mTagTextColor = tagTextColor;    }    public int getTagBgStyle() {        return mTagBgStyle;    }    public void setTagBgStyle(int tagBgStyle) {        mTagBgStyle = tagBgStyle;    }    public int getTagBgStrokeWidth() {        return mTagBgStrokeWidth;    }    public void setTagBgStrokeWidth(int tagBgStrokeWidth) {        mTagBgStrokeWidth = tagBgStrokeWidth;        paint.setStrokeWidth(dp2px(mTagBgStrokeWidth));        invalidate();    }    public int sp2px(int spVal) {        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, getResources().getDisplayMetrics());    }    public int dp2px(int dpVal) {        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics());    }}

接下来是自定义的属性

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="ColorTagView">        <attr name="tag_bg" format="color"></attr>   <!-- backgroud tagview   背景颜色-->        <attr name="tag_bg_style" format="enum">     <!-- tag style   显示样式-->            <enum name="FILL" value="0"></enum>            <enum name="STROKE" value="1"></enum>        </attr>        <attr name="tag_bg_stroke_width" format="dimension"></attr>   <!-- stoke width   -->        <attr name="tag_text" format="string"></attr>                <!--tag text -->        <attr name="tag_textSize" format="dimension"></attr>          <!-- tag text size  字体大小-->        <attr name="tag_textColor" format="color"></attr>             <!-- text color  字体颜色 -->        <attr name="tag_radius" format="dimension"></attr>            <!--tag  radius   -->    </declare-styleable></resources>

OK一个简单的tagview就OK了,同时详细用法—->colortagview 欢迎拍砖

阅读全文
0 0
原创粉丝点击