Android点划线自定义View
16lz
2021-01-26
试了很多网上的代码总是不行,最好的情况是在自己的布局文件里预览是正确的,运行到真机却显示一条直线
后来搞了很久终于搞定:
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><com.fhc.view.DashDotLineView *fhc:horizontal="true"/>*/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);}}}
更多相关文章
- android:Adapter中无法设置textview字体颜色(解决)
- Android(安卓)2.2 Eclipse 源码工程 调试
- Android(安卓)基础shape
- Android(安卓)repo/git server 建立过程
- android 修改AlertDialog的黑色背景的两种方式及圆角边框的设置
- rtl8188eu WiFi移植
- AndroidStudio导入旧版本工程若干问题
- Android开机运行,替换系统launch
- Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见