Android 自定义垂直虚线控件

2024-02-25 13:58

本文主要是介绍Android 自定义垂直虚线控件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

开发中遇到垂直虚线作为分割线,本以为使用shape可以解决,没想到shape实现的只有水平方面的虚线没有垂直方向,无奈只能自己写一个控件。

为了以后使用方便,将设置控件的样式提到外部实现。需要的同学也可以拿去使用

1.在attrs.xml文件中添加使用方法

<declare-styleable name="LineDashView"><!--虚线颜色--><attr name="color" format="color"/><!--虚线宽度--><attr name="dashWidth" format="dimension"/><!--虚线间隔--><attr name="dashGap" format="dimension"/><!--虚线方向 垂直还是水平--><attr name="line_orientation" format="integer"><!--水平虚线--><enum name="horizontal" value="0" /><!--垂直虚线--><enum name="vertical" value="1" /></attr></declare-styleable>

2.虚线view现实类

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;import com.zh.app.R;/*** Created by zhanghe on 2019/8/3.* 水平或者垂直虚线*/public class LineDashView extends View {private Paint mPaint;private int height;private int width;private Path mPath;private float orientation;public LineDashView(Context context) {this(context,null);}public LineDashView(Context context, @Nullable AttributeSet attrs) {this(context, attrs,0);}public LineDashView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LineDashView, defStyleAttr,0);float dashGap = a.getDimension(R.styleable.LineDashView_dashGap, 0);float dashWidth = a.getDimension(R.styleable.LineDashView_dashWidth, 0);orientation = a.getInt(R.styleable.LineDashView_line_orientation, 0);ColorStateList colorStateList = a.getColorStateList(R.styleable.LineDashView_color);a.recycle();colorStateList = colorStateList != null ? colorStateList : ColorStateList.valueOf(0xFF000000);int lineColor = colorStateList.getColorForState(getDrawableState(), 0);mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);mPaint.setColor(lineColor);mPaint.setStyle(Paint.Style.STROKE);DashPathEffect e = null;if (dashWidth > 0) {e = new DashPathEffect(new float[] { dashWidth, dashGap }, 0);}mPaint.setPathEffect(e);mPath = new Path();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);width = MeasureSpec.getSize(widthMeasureSpec);height = MeasureSpec.getSize(heightMeasureSpec);mPaint.setStrokeWidth(width);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (orientation == 0){//水平mPath.moveTo(0,0);mPath.lineTo(width,0);}else if (orientation == 1){//垂直mPath.moveTo(0,0);mPath.lineTo(0,height);}canvas.drawPath(mPath,mPaint);}
}

3.xml布局文件使用方式

<com.zh.app.widget.LineDashViewandroid:layout_width="1dp"android:layout_height="match_parent"app:color="@color/white"app:line_orientation="vertical"app:dashGap="2dp"app:dashWidth="4dp"/>

这篇关于Android 自定义垂直虚线控件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/745693

相关文章

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束