一个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
- 一个colortagview,告别繁琐的xml
- UMAX系统让您告别繁琐的操作流程
- Volley框架让你告别繁琐的多线程
- Android:一键find控件,从此告别繁琐的findViewById
- 使用velocity 代码生成器,告别繁琐的增删改查
- 想告别繁琐的工作?你可能需要这个。
- 一个比较繁琐的导航
- 告别世俗和平庸,告别臃肿和繁琐,拿来就用不必安装,这就是内存操作系统的魅力~~
- 告别繁琐的配置文件 ---- 全注解Spring,Hibernate开发 不完全指南
- Eclipse + android-ndk 实现编译so库 告别Cygwin之类的繁琐环境
- 手动修改Restorator源文件告别繁琐:
- 一行代码搞定Parcelable 告别Parcelable繁琐代码!
- 一行代码搞定Parcelable,告别Parcelable繁琐代码!
- 一行代码搞定Parcelable 告别Parcelable繁琐代码!
- php 繁琐的问题
- 离散--繁琐的定义
- (Android新手上路)actionbar完全自定义标题布局,告别繁琐的actionbar设置,看别人的然后自己琢磨出来的
- 告别jsp+servlet开发中,配置web.xml的烦恼
- 全国省市区数据SQL
- leetcode--Rotate List
- Activity intent经常使用的 FLAG 集合
- MongoDB常用操作一查询find方法db.collection_name.find()
- G
- 一个colortagview,告别繁琐的xml
- GitLab 之 PlantUML 的配置及使用
- 微信小程序-入门篇(app.js)
- 初始化
- SpringMVC教程书目录
- 在一个字符串中,找到第一个只出现一次的字符,例如:abaccdef,输出b
- IDEA 2017.2输入法候选框不跟随光标的问题 Windows 7 旗舰版
- Hadoop 2.6.4单节点环境搭建
- 将long数组转换成string数组