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

Android自定义view绘制虚线

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

别的不多说先看一眼效果

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

代码如下

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

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

  • 画水平虚线

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

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line">
        <solid android:color="@color/red"></solid>
        <stroke
            android:dashWidth="10dp" //虚线的小线段长度
            android:dashGap="10dp" //间隔距离
            android:color="@color/app_color"
            android:width="4dp"
            />
    </shape>

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

    <View
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_gravity="center_vertical"
        android:background="@drawable/dottde_line"
        android:layerType="software"  />  //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 .
     * 作者邮箱: [email protected]
     * 功能描述: 一个横向的虚线view
     * 类    名: HorizontalPartLineView
     * 备    注: 网络参考链接: http://blog.csdn.net/cxmscb/article/details/51760528
     * http://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

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--圆角图片imageview-->
    <declare-styleable name="Horizontal_PartLine_View">
        <!--实线属性-->
        <attr name="solid_width" format="dimension" />
        <attr name="solid_color" format="color" />
        <!--虚线属性,注意虚线是透明没有颜色的-->
        <attr name="hollow_width" format="dimension" />
        <!--控件的底色-->
        <attr name="bg_color" format="color"/>
        <attr name="offset" format="dimension"/>
    </declare-styleable>


</resources>

3.布局文件中使用

 <com.ztsc.house.customview.partline.HorizontalPartLineView
    android:layout_width="match_parent"
    android:layout_height="3px"
    app:solid_color="@color/color_hint_dcdcdc"
    app:bg_color="@color/color_bg_ffffff"
    app:solid_width="10px"
    app:hollow_width="10px" />

4.定义引用控件

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

猜你喜欢

转载自blog.csdn.net/bencheng06/article/details/79585452