android自定义View---生成虚线的View

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.最终运行效果如下:







  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值