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

相关文章

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.