在Android屏幕中绘制虚线,最通用的是自定义控件DashedLine,再将自定义控件放入xml布局中

运行截图:

程序结构

package com.example.asus.gary_042;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View; /**
* Created by ASUS on 2018/5/26.
*/ public class DashedLine extends View{
public DashedLine(Context context,AttributeSet attrs) {
super(context,attrs);
} protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
Path path = new Path();
path.moveTo(0,200);
path.lineTo(1280,200);
PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);
paint.setPathEffect(effects);
canvas.drawPath(path,paint);
}
}

DashedLine

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.asus.gary_042.MainActivity"> <com.example.asus.gary_042.DashedLine
android:id="@+id/dashedLine"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </LinearLayout>

activity_main.xml

一、自定义控件DashedLine,使用这个控件能在屏幕中绘制一条虚线

   protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint paint = new Paint();
//给path设置样式(效果)的,STORKE设置虚线
paint.setStyle(Paint.Style.STROKE);
//设置虚线颜色
paint.setColor(Color.BLACK);
Path path = new Path();
//起点
path.moveTo(0,200);
//终点
path.lineTo(1280,200);
//那么虚线的一个单位就是由5像素实线,5像素空白,5像素实线,5像素空白组成的虚线段。
PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);
//将样式放入直线中
paint.setPathEffect(effects);
canvas.drawPath(path,paint);
}

canvas.drawPath方法:传送门

Path类包含了由直线、二次曲线、三次曲线组成多种符合的集合路径图形,它可以用canvas.drawPath()来绘制,并且可以使填充的或者描边的(是基于paint的style的),并且它可以用于裁剪或者在一条path上面绘制文本。

Canvas只有drawLine方法,没有drawDashLine方法。但是你要知道,画什么虽然是Canvas决定的,但是怎么画却是由画笔Paint决定的

Paint有setPathEffect(PathEffect effect)这么一个方法,PathEffect一共有六个子类:传送门
ComposePathEffect,
CornerPathEffect,
DashPathEffect,
DiscretePathEffect,
PathDashPathEffect,
SumPathEffect,
其中的DashPathEffect就是我们需要的虚线效果

二、在activity_main文件中引入自定义控件DashedLine

 
添加引用该自定义空间所在位置的绝对路径
<com.example.asus.gary_042.DashedLine>
 
 <com.example.asus.gary_042.DashedLine
android:id="@+id/dashedLine"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

Android_(控件)使用自定义控件在屏幕中绘制一条虚线的更多相关文章

  1. Android利用已有控件实现自定义控件

    Android控件的基本介绍及使用自定义控件的意义         Android 本身提供了很多控件,自定义控件在android中被广泛运用,自定义控件给了我们很大的方便.比如说,一个视图为imag ...

  2. C#继承基本控件实现自定义控件

    C#继承基本控件实现自定义控件 摘自:http://www.cnblogs.com/greatverve/archive/2012/04/25/user-control-inherit.html 自定 ...

  3. Android学习Scroller(三)——控件平移划过屏幕 (Scroller简单使用)

    MainActivity例如以下: package cc.cn; import android.os.Bundle; import android.view.View; import android. ...

  4. 如何使用免费PDF控件从PDF文档中提取文本和图片

             如何使用免费PDF控件从PDF文档中提取文本和图片 概要 现在手头的项目有一个需求是从PDF文档中提取文本和图片,我以前也使用过像iTextSharp, PDFBox 这些免费的PD ...

  5. 因为此控件已在 web.config 中注册并且与该页位于同一个目录中

    在web.config文件配置了用户控件 <pages> <controls> <add tagPrefix="my" tagName="l ...

  6. 《ASP.NET1200例》解决母版页报错“内容控件必须是内容页中的顶级控件,或是引用母版页的嵌套母版页。”

    VS2005下,添加了母版页这个控件,我们可以讲N个页面中共同的部分放在母版页来实现,并让WEB窗体集成自我们的母版页,就可以让我们的站点具有统一的风格了.在VS2005SP1之前的版本中,我们只能创 ...

  7. VS2010 Chart控件(一)Chart控件在ASP.NET网站中的应用示例详解(C#语言)

    步骤如下: 1. Chart控件(一)Chart控件在ASP.NET网站中的应用示例详解(C#语言)" title="VS2010 Chart控件(一)Chart控件在ASP.NE ...

  8. 玩转控件:重绘DEVEXPRESS中DateEdit控件 —— 让DateEdit支持只选择年月 (提供源码下载)

      前言 上一篇博文<玩转控件:重绘ComboBox —— 让ComboBox多列显示>中,根据大家的回馈,ComboBox已经支持筛选了,更新见博文最后最后最后面.   奇葩 这两天遇到 ...

  9. MFC中给控件添加变量,DoDataExchange中

    DoDataExchange函数其实是一项数据动态绑定技术.比如你在写动态按钮过程中须对按钮添加变量时,怎么添加?控件类已经写好了,其变量是已经固定的.你要添加新的变量就要用到DoDataExchan ...

随机推荐

  1. ns nat rule

    ns nat rule NAT实现方式: NAT的实现方式有三种,即静态转换(Static Nat).动态转换(Dynamic Nat) 和 端口多路复用(OverLoad). 静态转换是指将内部网络 ...

  2. JS基础_对象字面量

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. JS基础_代码块

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. HTML5之历史记录(实现的当页面应用路由器的底层)

    history hashchange与popstate 一.history history.back():加载history列表中的前一个URL history.forward():加载history ...

  5. npm install 常用的几个参数

    npm install moduleName # 安装模块到项目目录下 npm install -g moduleName # -g 的意思是将模块安装到全局,具体安装到磁盘哪个位置,要看 npm c ...

  6. 微信小程序中button去除默认的边框

    button { position:relative; display:block; margin-left:auto; margin-right:auto; padding-left:14px; p ...

  7. Editplus code

    网上一大堆,垃圾也是一大堆,保留一个真正的,提高效率 原文链接:https://blog.csdn.net/anhldd/article/details/85088715 Vovan 3AG46-JJ ...

  8. linux常用命令(4)

    linux常用命令(4) --- Vim编辑器与Shell命令脚本 如何使用vim编辑器来编写文档.配置主机名称.网卡参数以及yum仓库: 通过vim编辑器将Linux命令放入合适的逻辑测试语句(if ...

  9. XSS防御和绕过1

    原理:对用户输入没做过滤和处理,是用户可以输入一些东西(例如js),控制输出达到一些攻击目的 1.DOM型 基于DOM的XSS有时也称为type0XSS.当用户能够通过交互修改浏览器页面中的DOM(D ...

  10. Redis总结2

    一.Redis效率高的原因 众所周知,Redis常用来做缓存,从而提高项目QPS(每秒查询率).QPS = 并发量 / 平均响应时间 然而其效率高的原因包含但不仅限于如下几点: 1.Redis基于内存 ...