自定义虚线 可定制 水平或竖直

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;
public class DashedLineView extends View{
    private Paint paint = null;
    private Path path = null;
    private PathEffect effects = null;

    private int orientation;
    private int lineColor;

    public static final int HORIZONTAL = 0;
    public static final int VERTICAL = 1;
    private static final int DEFAULT_COLOR_THEME = Color
            .parseColor("#ff000000");

    public DashedLineView(Context context) {
        super(context, null);
        // TODO Auto-generated constructor stub
    }

    public DashedLineView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        // TODO Auto-generated constructor stub
        setCustomAttributes(attrs);
    }

    public DashedLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
        setCustomAttributes(attrs);
    }

    private void setCustomAttributes(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.dashedline);
        lineColor = a.getColor(R.styleable.dashedline_lineColor,
                DEFAULT_COLOR_THEME);
        orientation = a.getInt(R.styleable.dashedline_orientation, HORIZONTAL);
        paint = new Paint();
        path = new Path();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(lineColor);
        paint.setAntiAlias(true);
        a.recycle();
    }

    @SuppressLint("DrawAllocation")
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        path.moveTo(0, 0);
//        if (orientation == VERTICAL) {
//            path.lineTo(0, this.getHeight());
//        } else {
//            path.lineTo(this.getWidth(), 0);
//        }
//        // PathEffect是用来控制绘制轮廓(线条)的方式
//        // 代码中的float数组,必须是偶数长度,且>=2,指定了多少长度的实线之后再画多少长度的空白.如本代码中,绘制长度5的实线,再绘制长度5的空白,再绘制长度5的实线,再绘制长度5的空白,依次重复.1是偏移量,可以不用理会.
//
        effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);
//        effects = new DashPathEffect(new float[]{8, 10, 8, 10}, 0);
//        paint.setPathEffect(effects);
//        canvas.drawPath(path, paint);

        float startY = getHeight();
        float startX = getWidth() / 2;
        DashPathEffect dashPathEffect =
                new DashPathEffect(new float[]{8, 10, 8, 10}, 0);
        paint.setPathEffect(dashPathEffect);

        Path path = new Path();
        path.moveTo(startX,0);
        path.lineTo(startX,startY);
        canvas.drawPath(path,paint);
    }

}

布局中应用

 <com.xxx.xxxx.widgets.DashedLineView
                android:layout_marginLeft="8dp"
                android:id="@+id/id_dash"
                android:layout_width="4dp"
                android:layout_height="match_parent"
                app:lineColor="@color/white"
                app:orientation="vertical"/>

自定义属性

 <declare-styleable name="dashedline">
        <attr name="lineColor" format="color"/>
        <attr name="orientation">
            <enum name="horizontal" value="0"/>
            <enum name="vertical" value="1"/>
        </attr>
    </declare-styleable>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值