自定义label组件

自定义label组件

支持边框绘制
支持shape背景(按指定圆角裁剪,矩形,圆角矩,圆形),支持指定角圆角
支持自定义阴影(颜色,偏移,深度)
边框颜色支持状态选择器

预览

在这里插入图片描述

核心绘制辅助类
public class LabelHelper {private final Paint paint;private Paint shadowPaint;private final float[] radiusList = new float[8];// 矩阵四角圆角 两个一组分别为一角的x轴半径y轴半径  四组分别为 上左 上右  下右  下左private final Rect rect;private final Path path;private final RectF rectF;private float strokeWidth;private ColorStateList strokeColor;private boolean hasRadius;//是否有圆角private int shadowColor;//是否有阴影private float shadowRadius;private float shadowDx;private float shadowDy;private Float contentInsetLeft = null;private Float contentInsetRight = null;private int defStrokeColor;LabelHelper(View view, Context context, AttributeSet attrs) {this(view, context, attrs, Color.TRANSPARENT);}LabelHelper(View view, Context context, AttributeSet attrs, @ColorInt int defStrokeColor) {this.defStrokeColor = defStrokeColor;TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LabelView);// 圆角float radius = array.getDimension(R.styleable.LabelView_android_radius, 0);float topLeftRadius = array.getDimension(R.styleable.LabelView_android_topLeftRadius, radius);float topRightRadius = array.getDimension(R.styleable.LabelView_android_topRightRadius, radius);float bottomLeftRadius = array.getDimension(R.styleable.LabelView_android_bottomLeftRadius, radius);float bottomRightRadius = array.getDimension(R.styleable.LabelView_android_bottomRightRadius, radius);// 阴影int shadowColor = array.getColor(R.styleable.LabelView_android_shadowColor, Color.TRANSPARENT);float shadowRadius = array.getFloat(R.styleable.LabelView_android_shadowRadius, 0.0f);float shadowDx = array.getFloat(R.styleable.LabelView_android_shadowDx, 0.0f);float shadowDy = array.getFloat(R.styleable.LabelView_android_shadowDy, 0.0f);if (array.hasValue(R.styleable.LabelView_android_contentInsetLeft)) {contentInsetLeft = array.getDimension(R.styleable.LabelView_android_contentInsetLeft, 0);}if (array.hasValue(R.styleable.LabelView_android_contentInsetRight)) {contentInsetRight = array.getDimension(R.styleable.LabelView_android_contentInsetRight, 0);}int anInt = array.getInt(R.styleable.LabelView_fillType, 1);// 边框strokeWidth = array.getDimension(R.styleable.LabelView_borderWidth, 0);try {strokeColor = array.getColorStateList(R.styleable.LabelView_borderColor);} catch (Exception e) {e.printStackTrace();}if (strokeColor == null) {strokeColor = ColorStateList.valueOf(array.getColor(R.styleable.LabelView_borderColor, this.defStrokeColor));}view.setSelected(array.getBoolean(R.styleable.LabelView_selected, false));array.recycle();paint = new Paint();paint.setAntiAlias(true);paint.setStyle(getPaintStyle(anInt));paint.setStrokeWidth(strokeWidth);rectF = new RectF();rect = new Rect();path = new Path();setRadiusPx(view, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius);view.setLayerType(LAYER_TYPE_HARDWARE, null);setShadow(shadowColor, shadowRadius, shadowDx, shadowDy);}// 设置阴影画笔private void setShadow(int shadowColor, float shadowRadius, float shadowDx, float shadowDy) {if (shadowPaint == null) {shadowPaint = new Paint();shadowPaint.setAntiAlias(true);shadowPaint.setStyle(Paint.Style.FILL);}this.shadowRadius = Math.max(shadowRadius, 0);this.shadowDx = shadowDx;this.shadowDy = shadowDy;this.shadowColor = shadowColor;shadowPaint.setColor(shadowColor);shadowPaint.setShadowLayer(this.shadowRadius, this.shadowDx, this.shadowDy, shadowColor);}private Paint.Style getPaintStyle(int type) {return switch (type) {case 0 -> Paint.Style.FILL;case 2 -> Paint.Style.FILL_AND_STROKE;default -> Paint.Style.STROKE;};}void setStrokeWidth(int dp) {strokeWidth = SizeUtils.dp2px(dp);paint.setStrokeWidth(strokeWidth);}void setStrokeColor(@ColorInt int boundColor) {this.strokeColor = ColorStateList.valueOf(boundColor);}void setFillType(Paint.Style style) {paint.setStyle(style);}void setRadiusPx(View view, float topLeft, float topRight, float bottomRight, float bottomLeft) {radiusList[0] = topLeft;radiusList[1] = topLeft;radiusList[2] = topRight;radiusList[3] = topRight;radiusList[4] = bottomRight;radiusList[5] = bottomRight;radiusList[6] = bottomLeft;radiusList[7] = bottomLeft;hasRadius = topLeft > 0 || topRight > 0 || bottomRight > 0 || bottomLeft > 0;float offsetPaddingL = 0;float offsetPaddingR = 0;float offsetPaddingT = 0;float offsetPaddingB = 0;boolean autoInsetLeft = contentInsetLeft == null;boolean autoInsetRight = contentInsetRight == null;if (hasRadius) {if (autoInsetLeft) {offsetPaddingL = Math.max(topLeft, bottomLeft) / 2f;} else if (contentInsetLeft != 0) {offsetPaddingL = contentInsetLeft;}if (autoInsetRight) {offsetPaddingR = Math.max(topRight, bottomRight) / 2f;} else if (contentInsetRight != 0) {offsetPaddingR = contentInsetRight;}}if (isDrawBorder(view)) {offsetPaddingL = Math.max(offsetPaddingL, strokeWidth);offsetPaddingR = Math.max(offsetPaddingL, strokeWidth);offsetPaddingT = strokeWidth;offsetPaddingB = strokeWidth;}view.setPadding((int) Math.max(view.getPaddingLeft(), offsetPaddingL),(int) Math.max(view.getPaddingTop(), offsetPaddingT),(int) Math.max(view.getPaddingRight(), offsetPaddingR),(int) Math.max(view.getPaddingBottom(), offsetPaddingB));setStroke();view.invalidate();}private void setStroke() {if (hasRadius) {paint.setStrokeCap(Paint.Cap.ROUND);paint.setStrokeJoin(Paint.Join.ROUND);} else {paint.setStrokeCap(Paint.Cap.BUTT);paint.setStrokeJoin(Paint.Join.MITER);}}void draw(Canvas canvas, View view) {boolean drawBorder = isDrawBorder(view);boolean isDrawShadow = isDrawShadow();if (hasRadius || drawBorder || isDrawShadow) {view.getDrawingRect(rect);path.reset();rectF.set(rect);path.addRoundRect(rectF, radiusList, Path.Direction.CW);path.close();if (isDrawShadow) {// 绘制阴影canvas.drawPath(path, shadowPaint);path.reset();rectF.left += Math.max(shadowRadius - shadowDx, 0);rectF.right -= Math.max(shadowRadius + shadowDx, 0);rectF.top += Math.max(shadowRadius - shadowDy, 0);rectF.bottom -= Math.max(shadowRadius + shadowDy, 0);path.addRoundRect(rectF, radiusList, Path.Direction.CW);path.close();}if (hasRadius && !(drawBorder && paint.getStyle() != Paint.Style.STROKE)) {// 形状裁剪canvas.clipPath(path);}}}void onDraw(Canvas canvas, View view) {if (isDrawBorder(view)) {Paint.Style style = paint.getStyle();if (style != Paint.Style.STROKE) {canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);}paint.setColor(getBoundColor(view));canvas.save();if (style != Paint.Style.FILL) {path.reset();rectF.left += (strokeWidth / 2f - 0.5f);rectF.top += (strokeWidth / 2f - 0.5f);rectF.right -= strokeWidth / 2f;rectF.bottom -= strokeWidth / 2f;path.addRoundRect(rectF, radiusList, Path.Direction.CW);path.close();}// 边框绘制canvas.drawPath(path, paint);canvas.restore();}}Paint getPaint() {return paint;}public Rect getRect() {return rect;}public Path getPath() {return path;}public RectF getRectF() {return rectF;}public float getStrokeWidth() {return strokeWidth;}public boolean isHasRadius() {return hasRadius;}private int getBoundColor(View view) {int color = this.defStrokeColor;if (strokeColor != null) {if (strokeColor.isStateful()) {color = strokeColor.getColorForState(view.getDrawableState(), strokeColor.getDefaultColor());} else {color = strokeColor.getDefaultColor();}}return color;}private boolean isDrawBorder(View view) {return strokeWidth > 0 && getBoundColor(view) != Color.TRANSPARENT;}private boolean isDrawShadow() {return shadowPaint != null && shadowColor != Color.TRANSPARENT && shadowRadius > 0;}
}
自定义控件示例
public class LabelFrameLayout extends FrameLayout {private LabelHelper helper;public LabelFrameLayout(Context context) {this(context, null);}public LabelFrameLayout(Context context, AttributeSet attrs) {this(context, attrs, 0);}public LabelFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setWillNotDraw(false);helper = new LabelHelper(this, context, attrs);}public void setFillType(Paint.Style style) {if (helper != null && helper.getPaint() != null) {helper.setFillType(style);invalidate();}}public void setStrokeColor(@ColorInt int boundColor) {if (helper != null) {helper.setStrokeColor(boundColor);invalidate();}}public void setStrokeColorRes(@ColorRes int colorRes) {if (helper != null) {helper.setStrokeColor(getResources().getColor(colorRes));invalidate();}}public void setStrokeWidth(int dp) {if (helper != null && helper.getPaint() != null) {helper.setStrokeWidth(dp);invalidate();}}public void setRadius(int radiusDp) {setRadiusPx(SizeUtils.dp2px(radiusDp));}public void setRadiusPx(float radius) {setRadiusPx(radius, radius, radius, radius);}public void setRadius(float topLeft, float topRight, float bottomRight, float bottomLeft) {setRadiusPx(SizeUtils.dp2px(topLeft), SizeUtils.dp2px(topRight), SizeUtils.dp2px(bottomRight), SizeUtils.dp2px(bottomLeft));}public void setRadiusPx(float topLeft, float topRight, float bottomRight, float bottomLeft) {if (helper != null) {helper.setRadiusPx(this, topLeft, topRight, bottomRight, bottomLeft);}}@Overridepublic void invalidate() {if (isLaidOut()) {super.invalidate();}}@Overridepublic void draw(Canvas canvas) {if (helper != null) {helper.draw(canvas, this);}super.draw(canvas);}@Overrideprotected void onDraw(Canvas canvas) {if (helper != null) {helper.onDraw(canvas, this);}super.onDraw(canvas);}
}
xml使用示例
<包名.LabelFrameLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="100dp"android:layout_marginBottom="10dp"android:background="@color/subColorGray"app:borderColor="@color/red"app:borderWidth="1dp"android:radius="20dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="阿迪斯发斯蒂芬" /></包名.LabelFrameLayout>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/211025.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

二叉树题目:具有所有最深结点的最小子树

文章目录 题目标题和出处难度题目描述要求示例数据范围 解法一思路和算法代码复杂度分析 解法二思路和算法代码复杂度分析 题目 标题和出处 标题&#xff1a;具有所有最深结点的最小子树 出处&#xff1a;865. 具有所有最深结点的最小子树 难度 5 级 题目描述 要求 给定…

性能测试 —— 性能指标解读与性能分析!

性能测试监控关键指标: 1、系统指标:与⽤户场景与需求直接相关的指标 2、服务器资源指标:硬件服务器的资源使⽤情况的指标 3、JAVA应⽤ : JAVA应⽤程序在运⾏时的各项指标 4、数据库:数据库服务器运⾏时需要监控的指标 5、压测机资源指标:测试机在模拟⽤户负载时的资源使⽤…

VM CentOS7安装ffmpeg

项目中涉及给视频添加水印&#xff0c;使用到了ffmpeg&#xff0c;windows系统可直接使用&#xff0c;Linux需要手动编译完成ffmpeg后才可正常使用。 配置yum源: 备份原repo文件 cd /etc/yum.repos.d/mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.r…

免费图书教材配套资料:Spark大数据技术与应用(第2版)

《Spark大数据技术与应用&#xff08;第2版&#xff09;》课程内容全面介绍了Spark大数据技术的相关知识&#xff0c;内容包含包括Spark概述、Scala基础、Spark编程、Spark编程进阶、Spark SQL结构化数据文件处理、Spark Streaming实时计算框架、Spark GraphX图计算框架、Spark…

推荐一个简单的在线压缩PNG和JPG图片大小的网址

问题描述&#xff1a;推荐一个简单的在线压缩PNG和JPG图片大小的网址 解决&#xff1a; https://www.iloveimg.com/zh-cn/compress-image/compress-png

低代码平台推荐:五大低代码厂商谁的模式更“合适”

随着数字化时代的到来&#xff0c;低代码开发平台作为提高数字生产力的工具正受到越来越多企业的关注&#xff0c;市面上的低代码产品和厂商更是“乱花渐欲迷人眼”。 各家产品不仅功能各有不同&#xff0c;甚至商机都有区别的情况&#xff0c;如何做好产品选型已然成了采购企…

实时截留抖音询价的用户:10个合规方法,让你的业务迅速增长!

先来看实操成果&#xff0c;↑↑需要的同学可看我名字↖↖↖↖↖&#xff0c;或评论888无偿分享 一、引言 随着抖音的普及度越来越高&#xff0c;越来越多的商家开始关注抖音询价用户。这些潜在客户对于企业的发展至关重要&#xff0c;如何实时截留这些用户成为商家关注的重点…

软文推广中如何提炼好产品卖点,媒介盒子分享

内容同质化的时代下&#xff0c;企业应该如何让用户留下印象&#xff0c;并且成功将产品卖出去&#xff0c;核心思维就在于提炼产品卖点&#xff0c;产品卖点是销量提升的关键&#xff0c;相信企业在推广产品时都会有点困惑&#xff0c;感觉自家产品和竞品比起来只是logo、外观…

外设——CAN总线收发器TJA1043

目录 1. 引脚 2. 工作模式 3. 5种模式和7种状态标识的理解和使用 1. 引脚 2. 工作模式 该收发器相较于普通收发器&#xff0c;引脚多了几个&#xff0c;就是功能等多了。TJA1043支持五种操作模式&#xff0c;就是通过控制引脚STB_N和EN来原则。五种模式&#xff1a; 正常模式…

Flink 替换 Logstash 解决日志收集丢失问题

在某客户日志数据迁移到火山引擎使用 ELK 生态的案例中&#xff0c;由于客户反馈之前 Logstash 经常发生数据丢失和收集性能较差的使用痛点&#xff0c;我们尝试使用 Flink 替代了传统的 Logstash 来作为日志数据解析、转换以及写入 ElasticSearch 的组件&#xff0c;得到了该客…

河北专升本(微机原理)

第一章&#xff1a;计算机基础与数制转化 1. 进制运算基础 进位计数制: 按照进位的方法进行计数&#xff0c;称为进位计数制。常见的进位计数制有&#xff1a;二进制、八进制、十进制、十二进制、十六进制等等。 R进制数的特点&#xff1a; 1、具有R个不同的数符。0&#…

Linux使用宝塔面板+Discuz+cpolar内网穿透工具搭建可公网访问论坛

Linux宝塔面板搭建Discuz论坛&#xff0c; 并内网穿透实现公网访问 文章目录 Linux宝塔面板搭建Discuz论坛&#xff0c; 并内网穿透实现公网访问前言1.安装基础环境2.一键部署Discuz3.安装cpolar工具4.配置域名访问Discuz5.固定域名公网地址6.配置Discuz论坛 前言 Crossday Di…