试了很多网上的代码总是不行,最好的情况是在自己的布局文件里预览是正确的,运行到真机却显示一条直线

后来搞了很久终于搞定:

packagecom.fhc.view;importcom.fhc.android.R;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Bitmap;importandroid.graphics.Canvas;importandroid.graphics.DashPathEffect;importandroid.graphics.Paint;importandroid.util.AttributeSet;importandroid.view.View;/***点划线*@authorfanhoucheng*@Emailfanhoucheng@sina.com*@版权所有Copyright©2014fanhoucheng,AllRightsReserved.</br>*@使用说明可在xml中设置horizontal属性,以描绘横线还是纵线*</br>需要修改点划线样式请修改DashPathEffect(newfloat[]{3,3},0)中的数值*xmlns:fhc="http://schemas.android.com/apk/res/com.fhc.android*</br>&lt;com.fhc.view.DashDotLineView&nbsp;&nbsp;*fhc:horizontal="true"/&gt;*/publicclassDashDotLineViewextendsView{Contextcontext;booleanhorizontal=false;publicDashDotLineView(Contextcontext){super(context);this.context=context;}publicDashDotLineView(Contextcontext,AttributeSetattrs){super(context,attrs);init(context,attrs);this.context=context;if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.HONEYCOMB){setLayerType(View.LAYER_TYPE_SOFTWARE,null);}}privatevoidinit(Contextcontext,AttributeSetattrs){if(attrs!=null){TypedArraya=context.getTheme().obtainStyledAttributes(attrs,R.styleable.DashDotLineView,0,0);horizontal=a.getBoolean(R.styleable.DashDotLineView_horizontal,false);}}@OverrideprotectedvoiddispatchDraw(Canvascanvas){super.dispatchDraw(canvas);PaintmLinePaint=newPaint();Bitmapbackground=Bitmap.createBitmap(getWidth(),getHeight(),Bitmap.Config.ARGB_4444);canvas.drawBitmap(background,0,0,null);//画虚线mLinePaint.setColor(this.context.getResources().getColor(R.color.black));DashPathEffecteffect=newDashPathEffect(newfloat[]{3,3},0);mLinePaint.setAntiAlias(true);mLinePaint.setPathEffect(effect);mLinePaint.setStyle(Paint.Style.STROKE);mLinePaint.setStrokeWidth(2);mLinePaint.setSubpixelText(true);if(horizontal){canvas.drawLine(0,0,getWidth(),0,mLinePaint);}else{canvas.drawLine(0,0,0,getHeight(),mLinePaint);}}}

更多相关文章

  1. android:Adapter中无法设置textview字体颜色(解决)
  2. Android(安卓)2.2 Eclipse 源码工程 调试
  3. Android(安卓)基础shape
  4. Android(安卓)repo/git server 建立过程
  5. android 修改AlertDialog的黑色背景的两种方式及圆角边框的设置
  6. rtl8188eu WiFi移植
  7. AndroidStudio导入旧版本工程若干问题
  8. Android开机运行,替换系统launch
  9. Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见

随机推荐

  1. android viewflipper和tabhost效率对比
  2. Timber 源码解析
  3. Android使用Fragment来实现TabHost的功能
  4. Android(安卓)代码风格规范
  5. 【android之ScrollView滚动视图】
  6. 惹的祸
  7. Android处理按钮重复点击事件
  8. Android学习整理- 9- 实时更新UI-Handler
  9. Android(安卓)架构
  10. android编译源码+在源码中build工程方法(