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. 高性能双端js模板---simplite

    simplite是一款js实现的模板引擎,它能够完成浏览器端js模版和node服务器端js模板的数据渲染,渲染性能达到引擎的极限. 渲染性能十分突出. 支持浏览器端和node服务器端模板渲染. 它简单 ...

  2. C语言编写Windows服务程序

    原文:C语言编写Windows服务程序 #include <Windows.h> #include <stdio.h> #define SLEEP_TIME 5000 // 间 ...

  3. server正式的环境性能测试nginx-php 指着寻求突破的表现

    因为我是第三级城市语言.无法接触到更牛接触逼公司或环境.这是你母亲的现场环境摸过几次.截至完毕,测试已设法提高空间. 公司须要的站点执行环境.不能由于我这边的瓶颈而阻碍了公司进行,希望各位大能能不吝惜 ...

  4. orleans开篇之hello world

    orleans开篇之hello world 什么是orleans Orleans是一个建立在.NET之上的,设计的目标是为了方便程序员开发需要大规模扩展的云服务.Orleans项目基本上被认为是并行计 ...

  5. uploadify.js

    基于uploadify.js实现多文件上传和上传进度条的显示 uploadify是JQuery的一个插件,主要实现文件的异步上传功能,可以自定义文件大小限制.文件类型.是否自动上传等属性,可以显示上传 ...

  6. Shell的学习就从重装系统开始吧

    小标题:与Fedora的爱恨情仇 干巴巴的shell学习实在枯燥,看来学习姿势还是要从实践入手 起因:传说中的不作死就不会死,昨晚偶遇一本PDF,讲glade编辑界面的,以下就被吸引了,跟着讲解搞出个 ...

  7. C#放缩、截取、合并图片并生成高质量新图的类

    原文:C#放缩.截取.合并图片并生成高质量新图的类 using System;using System.Drawing;using System.Drawing.Imaging;using Syste ...

  8. 动动手,写个knockout的分页模板

    最近一个项目用ASP.NET + knockout开发,很多列表页面都带分页,于是就有了写一个公共的分页模板的想法. 先把template写好: <script type="text/ ...

  9. Python3.4入门之ifelse错误解决方案

    笔者用的是Python3.4 开始接触到ifelse语句 发现这样---C:\Users\Administrator>python e:\Python34\ifelse.py--去执行的时候老是 ...

  10. DesignMode的状态处理

    自定义控件开发的调试及DesignMode的状态处理 在开发Winform程序的时候,我们往往需要根据需要做一些自定义的控件模块,这样可以给系统模块重复利用,或者实现更好的效果等功能.但在使用的时候, ...