在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"
/>

最新文章

  1. 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度
  2. Android项目——传感器的使用
  3. log4j 使用笔记整理中
  4. http://www.cnblogs.com/
  5. php中函数内使用static修饰变量
  6. C语言工具的简单辨析
  7. 使用html5兼容低版本浏览器
  8. 测试 windows live writer
  9. longlistselector 闪烁问题研究
  10. viewer.js的简单练习
  11. 浅谈JavaScript的事件(事件模拟)
  12. 网站开发进阶(二十)JS中window.alert()与alert()的区别
  13. 连接Access数据遇到的问题总览!
  14. css预编译语言 sass scss(变量$var, css嵌套规则,@import规则,@extend,@mixin)
  15. Yarn Node Labels
  16. maven——依赖管理
  17. 重识linux-SSH中的SFTP
  18. Spring Boot多数据源配置与使用
  19. 使用tk.mybatis快速开发curd
  20. Spring学习(二)--IOC

热门文章

  1. 2-Perl 环境安装
  2. Fonour.AspnetCore 生成SQL SERVER数据库
  3. mvc验证码图片生成
  4. 对接外网post,get接口封装类库
  5. C 中 char、signed char 和 unsigned char 的区别
  6. PHP获取某段文字作为标题
  7. OO方式实现ALV: cl_salv_table
  8. (๑•̀ㅂ•́)و✧QQ用户信息管理系统
  9. Vagrant box 增加磁盘容量方法
  10. web录音——上传录音文件