1.在res/values/目录下 新建文件 attrs.xml

<?
 xml
 
version
=
 "1.0"
 
encoding

"utf-8"
 ?>
<
 resources
>
 
    
<
declare-styleable 
name 
=
 "dashedline"
>
        
<
attr 
name 
=
 "lineColor"
 
format
=
 "color"
 
/>
    
</
declare-styleable 
>
 
</
 resources
>

 
2.创建自定义View,DashedLine.java

public
 
class 
DashedLine 
extends
 View {
           
private
 Paint 
paint
 = 
null
;
           
private
 Path 
path
 = 
null
;
           
private
 PathEffect 
pe
 = 
null
;
 
           
public
 DashedLine(Context paramContext) {
                    
this
(paramContext, 
null
);
          }
 
           
public
 DashedLine(Context paramContext, AttributeSet paramAttributeSet) {
                    
super
(paramContext, paramAttributeSet);
    
               
  //通过R.styleable.dashedline获得我们在attrs.xml中定义的
//<declare-
styleable 
name="
dashedline
"> 
TypedArray

                   TypedArray a = paramContext.obtainStyledAttributes(paramAttributeSet, R.styleable.
 dashedline
);
                    
//我们在attrs.xml中<declare-
styleable 
name="
dashedline
">节点下
//添加了<attr name="lineColor" format="color" />

                    
//表示这个属性名为lineColor类型为color。当用户在布局文件中对它有设定值时
                    
//可通过TypedArray获得它的值当用户无设置值是采用默认值0XFF00000

                    
int
 lineColor = a.getColor(R.styleable. 
dashedline_lineColor
, 0XFF000000);
                   a.recycle();
                    
this

paint
 = 
new
 Paint();
                    
this

path
 = 
new
 Path();
                    
this

paint
.setStyle(Paint.Style. 
STROKE
);
                    
this

paint
.setColor(lineColor);
                    
this

paint
.setAntiAlias( 
true
);
                    
this

paint
.setStrokeWidth(BitmapHelper. 
dip2px
(getContext(), 2.0F));
                    
float
[] arrayOfFloat = 
new
 
float
[4];
                   arrayOfFloat[0] = BitmapHelper. 
dip2px
(getContext(), 2.0F);
                   arrayOfFloat[1] = BitmapHelper. 
dip2px
(getContext(), 2.0F);
                   arrayOfFloat[2] = BitmapHelper. 
dip2px
(getContext(), 2.0F);
                   arrayOfFloat[3] = BitmapHelper. 
dip2px
(getContext(), 2.0F);
                    
this

pe
 = 
new
 DashPathEffect(arrayOfFloat, BitmapHelper.
dip2px
(getContext(), 1.0F));
          }
 
           
@Override
           
protected
 
void
 onDraw(Canvas canvas) {
                    
super
.onDraw(canvas);
                    
this

path
.moveTo(0.0F, 0.0F);
                    
this

path
.lineTo(getMeasuredWidth(), 0.0F);
                    
this

paint
.setPathEffect( 
this

pe
);
                   canvas.drawPath( 
this

path

this

paint
);
          }
 
}
3.新建布局文件dashedline.xml

<?
 xml
 
version
=
 "1.0"
 
encoding

"utf-8"
 ?>

<
 LinearLayout
 
xmlns:android

"http://schemas.android.com/apk/res/android"
    
xmlns:dash
=
 "http://schemas.android.com/apk/res/com.example.martixtest"
    
android:layout_width
=
 "fill_parent"
    
android:layout_height
=
 "fill_parent"
    
android:background
=
 "#eee"
    
android:orientation
=
 "vertical"
 
>
 
    
<
com.example.martixtest.myview.DashedLine
        
android:layout_width

"fill_parent"
        
android:layout_height

"1dip"
        
android:layout_margin

"10dip"
        
dash:lineColor

"#ff50f8"
 
/>
 
    
<
com.example.martixtest.myview.DashedLine
        
android:layout_width

"fill_parent"
        
android:layout_height

"1dip"
        
android:layout_margin

"10dip"
/>
 
    
<
com.example.martixtest.myview.DashedLine
        
android:layout_width

"fill_parent"
        
android:layout_height

"1dip"
        
android:layout_margin

"10dip"
        
dash:lineColor

"#f34f71"
 
/>
 
</
 LinearLayout
>
4.最终运行效果如下:






android自定义View---生成虚线的View的更多相关文章

  1. android自定义View_0——Create a custom view class

    一:创建一个自定义view的原则: 1.符合android的标准 2.能在layout的xml中定义控件属性 3.实现些新功能 4.要兼容android的大多数版本 二:定义一个view的子类 1.可 ...

  2. Android 自定义录音、播放动画View,让你的录音浪起来

    最近公司项目有一个录音的录制和播放动画需求,然后时间是那么紧,那么赶紧开撸. 先看效果图 嗯,然后大致就是这样,按住录音,然后有一个倒计时,最外层一个进度条,还有一个类似模拟声波的动画效果(其实中间的 ...

  3. Android 自定义 View 圆形进度条总结

    Android 自定义圆形进度条总结 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 微信公众号:牙锅子 源码:CircleProgress 文中如有纰漏,欢迎大家留言指出. 最近 ...

  4. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

  5. [原] Android 自定义View步骤

    例子如下:Android 自定义View 密码框 例子 1 良好的自定义View 易用,标准,开放. 一个设计良好的自定义view和其他设计良好的类很像.封装了某个具有易用性接口的功能组合,这些功能能 ...

  6. Android自定义View

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View ...

  7. Android 自定义View (一)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View ...

  8. Android 自定义View之BounceProgressBar

    之前几天下载了很久没用了的桌面版酷狗来用用的时候,发现其中加载歌曲的等待进度条的效果不错(个人感觉),如下: 然后趁着这周末两天天气较冷,窝在宿舍放下成堆的操作系统作业(目测要抄一节多课的一堆堆文字了 ...

  9. Android自定义View和控件之一-定制属于自己的UI

    照例,拿来主义.我的学习是基于下面的三篇blog.前两是基本的流程,第三篇里有比较细致的绘制相关的属性.第4篇介绍了如何减少布局层次来提高效率. 1. 教你搞定Android自定义View 2. 教你 ...

随机推荐

  1. OpenStreetMap架构

    OpenStreetMap框架简介 1.OSM平台开发 OpenStreetMap(缩写OSM)地图是一个合作项目,我们的目标是创建一个免费的内容,让所有的人都可以编辑的世界地图. OSM在地图上由一 ...

  2. C#6.0 中的那些新特性

    C#6.0 中的那些新特性 前言 VS2015在自己机器上确实是装好了,费了老劲了,想来体验一下跨平台的快感,结果被微软狠狠的来了一棒子了,装好了还是没什么用,应该还需要装Xarmain插件,配置一些 ...

  3. Redis3

    Redis到底该如何利用 上两篇受益匪浅,秉着趁热打铁,不挖到最深不罢休的精神,我决定追加这篇.上一篇里最后我有提到实现分级缓存管理应该是个可行的方案,因此今天特别实践了一下.不过缓存分级之后也发现了 ...

  4. ubuntu下的apache的虚拟主机的配置

    ubuntu下的虚拟主机的配置相对window下的虚拟主机配置有些许不同. 对于要新建的虚拟主机,我们可以有如下几个步骤: 1.在/etc/apache2/sites-available/目录下新建一 ...

  5. 读书笔记—CLR via C#同步构造28-29章节

    前言 这本书这几年零零散散读过两三遍了,作为经典书籍,应该重复读反复读,既然我现在开始写博了,我也准备把以前觉得经典的好书重读细读一遍,并且将笔记整理到博客中,好记性不如烂笔头,同时也在写的过程中也可 ...

  6. Talend open studio数据导入、导出、同步Mysql、oracle、sqlserver简单案例

    推荐大家一个BI工具:talend open studio.我也是刚接触,懂得不多,感觉比较神奇就想大家推荐一下... 由于公司项目,接触了一下BI工具talend,感觉功能很强大, 可以同步多种数据 ...

  7. Routing(路由) & Multiple Views(多个视图) step 7

    Routing(路由) & Multiple Views(多个视图) step 7 1.切换分支到step7,并启动项目 git checkout step-7 npm start 2.需求: ...

  8. 在线web编辑器

    真正在线编辑的在线web编辑器 最近正在研究开发一款在线web编辑器架构,这是一款真正傻瓜式的web编辑器,可以在正常浏览页面的情况进行编辑,经过测试,对于一般网页页面来说非常好用方便,操作更简单. ...

  9. C#基础知识梳理索引

    C#基础知识梳理索引 一 引子 之前曾写了一篇随笔<.NET平台技术体系梳理+初学者学习路径推荐+我们的愿景与目标> 三个月过去了,目标使更多的编程初学者,轻松高效地掌握C#开发的基础,重 ...

  10. const与readonly的区别

    const与readonly 很像,都是将变量声明为只读,且在变量初始化后就不可改写.那么,const与readonly 这两个修饰符到底区别在什么地方呢?其实,这个牵扯出C#语言中两种不同的常量类型 ...