当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义view的方式绘制虚线

Android自定义view的方式绘制虚线

2018年03月18日  | 移动技术网IT编程  | 我要评论

尊信理财,麦芽糖 美影社,猪腰的做法

Android自定义view绘制虚线

最近项目中有个需求,通过自定义view的方式绘制虚线

别的不多说先看一眼效果

\

这个需求在我们的开发中应该是一个很常见的需求了吧,有人会说有更简单的实现方式,对,但是你试过么,比如网上提到的这种方式

代码如下

这里写图片描述
看样子好像已经成功了,最后在把这个作为background加进去,

结果在真机上跑一圈,发现显示的竟然是一条实线,在好几台手机上试都是这样,都是一条实线,后来查资料发现需要加上一句话
android:layerType=”software” . android:shape="line"完美解决

画水平虚线

1.直接建一个shape,设置stroke属性就行了,再将这个属性直接作为background的drawable属性引入就行了
注意在4.0以上的真机加一句

    
    
        
        
    

2.布局xml文件中直接引用

      //4.0以上的加,不然真机中是实线

我的手机是华为 p9 系统7.0 最终发现问题没有效果,后来调整了 android:layout_height="1dp" 才发现,这样绘制出来的横向直线确实是成功了,但是是包裹在view四周的,这种先天性的缺陷导致了他无法在高度特别低的控件中使用

\

添加这句话就可以解决 android:shape="line"

最终的效果:

\

其实我们可以通过自定义一个view的形式来实现这个效果

1.自定义一个java类继承view

    /**
     * Created by benchengzhou on 2018/3/16  15:47 .
     * 作者邮箱: mappstore@163.com
     * 功能描述: 一个横向的虚线view
     * 类    名: HorizontalPartLineView
     * 备    注: 网络参考链接: https://blog.csdn.net/cxmscb/article/details/51760528
     * https://blog.csdn.net/xmxkf/article/details/51468648
     * https://www.cnblogs.com/prophet-it/p/6651033.html
     */

    public class HorizontalPartLineView extends View {

        private int DEFAULT_SOLID_WIDTH = 1;
        private int DEFAULT_HOLLOW_WIDTH = 1;
        private int DEFAULT_BG_COLOR = 0xffffffff;
        private int DEFAULT_SOLID_COLOR = 0xffdcdcdc;


        private int SolidW;
        private int hollowW;
        private int SolidColor;
        private int bgColor;
        private int offset=0;
        private Paint mPaint;

        private int DEFAULT_LINE_HEIGHT = 30;
        private int mWidth;
        private int mHeight;

        public HorizontalPartLineView(Context context) {
            this(context, null);
        }

        public HorizontalPartLineView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }

        public HorizontalPartLineView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);

            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Horizontal_PartLine_View, defStyleAttr, 0);

            //边框线宽
            SolidW = a.getDimensionPixelSize(R.styleable.Horizontal_PartLine_View_solid_width, DEFAULT_SOLID_WIDTH);
            hollowW = a.getDimensionPixelSize(R.styleable.Horizontal_PartLine_View_hollow_width, DEFAULT_HOLLOW_WIDTH);
            SolidColor = a.getColor(R.styleable.Horizontal_PartLine_View_solid_color, DEFAULT_SOLID_COLOR);
            bgColor = a.getColor(R.styleable.Horizontal_PartLine_View_bg_color, DEFAULT_BG_COLOR);
            offset = a.getColor(R.styleable.Horizontal_PartLine_View_offset, 0);


            if (SolidW % 2 != 0) {
                SolidW++;
            }
            if (hollowW % 2 != 0) {
                hollowW++;
            }

            if (SolidW == 0) {
                new Throwable(new IllegalArgumentException("the value SolidW san not be zone !"));
            }
            if (hollowW == 0) {
                new Throwable(new IllegalArgumentException("the value hollowW san not be zone !"));
            }

            a.recycle();   //使用完成之后记得回收
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
             mWidth = MeasureSpec.getSize(widthMeasureSpec);
    //        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
             mHeight = MeasureSpec.getSize(heightMeasureSpec);
    //        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(bgColor);
            canvas.drawRect(0, 0, mWidth, mHeight, paint);



            mPaint = new Paint();
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(SolidColor);
            mPaint.setAntiAlias(true);
            mPaint.setStrokeWidth(/*dip2px(ctx,2)*/mHeight);
            Path path = new Path();
            path.moveTo(0, 0);
            path.lineTo(mWidth, 0);
            PathEffect effects = new DashPathEffect(new float[]{SolidW, hollowW, SolidW, hollowW}, offset);
            mPaint.setPathEffect(effects);
            canvas.drawPath(path, mPaint);
        }


    }

2.创建自定义属性文件 attrs-line.xml



    
    
        
        
        
        
        
        
        
        
    



3.布局文件中使用

 

4.定义引用控件

    xmlns:app="https://schemas.android.com/apk/res-auto"

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网