Android虚线View

package com.sfbest.mapp.mipad.common.view;

import com.sfbest.mapp.mipad.R;

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;

/**
 * Created by liu on 15/4/22.
 *
 */
/**
 *        ┏┓   ┏┓+ +
 *       ┏┛┻━━━┛┻┓ + +
 *       ┃       ┃  
 *       ┃   ━   ┃ ++ + + +
 *       ████━████ ┃+
 *       ┃       ┃ +
 *       ┃   ┻   ┃
 *       ┃       ┃ + +
 *       ┗━┓   ┏━┛
 *         ┃   ┃           
 *         ┃   ┃ + + + +
 *         ┃   ┃    Code is far away from bug with the animal protecting       
 *         ┃   ┃ +     神兽保佑,代码无bug  
 *         ┃   ┃
 *         ┃   ┃  +         
 *         ┃    ┗━━━┓ + +
 *         ┃        ┣┓
 *         ┃        ┏┛
 *         ┗┓┓┏━┳┓┏┛ + + + +
 *          ┃┫┫ ┃┫┫
 *          ┗┻┛ ┗┻┛+ + + +
 */
public class LineView extends View {
    private static final int DEFAULT_LINE_COLOR = Color.BLACK;

    private static final boolean DEFAULT_LINE_STYLE = true;


    private int lineColor = DEFAULT_LINE_COLOR;
    /**
     * 水平虚线,还是垂直虚线,true为水平,false为垂直,默认为true(水平)
     */
    private boolean lineStyle = DEFAULT_LINE_STYLE;

    private int top;
    private int left;
    private int right;
    private int bottom;


    public LineView(Context context) {
        super(context);
    }

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

    public LineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.LineView, defStyleAttr, 0);


        lineColor = a.getColor(R.styleable.LineView_line_color,
                DEFAULT_LINE_COLOR);
        lineStyle = a.getBoolean(R.styleable.LineView_line_style, DEFAULT_LINE_STYLE);
        a.recycle();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(lineColor);
        Path path = new Path();

        if (lineStyle) {
            path.moveTo(0, getHeight() / 2);
            path.lineTo(getWidth(), getHeight() / 2);
        } else {
            path.moveTo(getWidth() / 2, 0);
            path.lineTo(getWidth() / 2, getHeight());
        }
        PathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
        paint.setPathEffect(effects);
        canvas.drawPath(path, paint);

    }


}


代码链接
 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值