Android 简单实现 虚线View

在Android上使用虚线总会有各种麻烦事情。这里贴一个简单的自定义View实现虚线的代码。

目前写好的可自定义内容:

  • 线宽
  • 线的颜色
  • 每节虚线的宽度
  • 每节虚线的间隔
  • 方向
  • 有其他需求的小伙伴自行添加吧,代码挺简单的。

java代码:

/**
 * Created by CZH on 2017/6/12.
 * 虚线
 */
public class ImaginaryLine extends View {

    private Paint linePaint;
    private int orientation;
    private Path path;

    public ImaginaryLine(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public ImaginaryLine(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ImaginaryLine);
        int lineColor = typedArray.getColor(R.styleable.ImaginaryLine_lineColor, Color.parseColor("#CACACA"));
        float lineWidth = typedArray.getDimension(R.styleable.ImaginaryLine_lineWidth, 10);
        float imaginaryWidth = typedArray.getDimension(R.styleable.ImaginaryLine_imaginary_width, 5);
        float intervalWidth = typedArray.getDimension(R.styleable.ImaginaryLine_interval_width, 5);
        orientation = typedArray.getInteger(R.styleable.ImaginaryLine_lineOrientation, 2);
        typedArray.recycle();
        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setColor(lineColor);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(lineWidth);
        PathEffect effects = new DashPathEffect(new float[]{imaginaryWidth, intervalWidth}, 0.0f);//设置虚线的间隔和点的长度
        linePaint.setPathEffect(effects);
    }

    @Override
    public void layout(@Px int l, @Px int t, @Px int r, @Px int b) {
        super.layout(l, t, r, b);
        if (path != null)
            path.reset();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (path == null) {
            path = new Path();
        }
        if (path.isEmpty()) {
            if (orientation == 1) {
                path.moveTo(getMeasuredWidth() / 2, 0);
                path.lineTo(getMeasuredWidth() / 2, getMeasuredHeight());
            } else {
                path.moveTo(0, getMeasuredHeight() / 2);
                path.lineTo(getMeasuredWidth(), getMeasuredHeight() / 2);
            }
        }

        canvas.drawPath(path, linePaint);
    }

    public void setLineColor(int color) {
        linePaint.setColor(color);
        invalidate();
    }

}

attrs:

 <!--虚线-->
  <declare-styleable name="ImaginaryLine">
     <!--方向-->
    <attr name="lineOrientation">
      <enum name="vertical" value="1" />
      <enum name="horizontal" value="2" />
    </attr>
    <!--颜色-->
    <attr name="lineColor" format="color" />
    <!--线宽-->
    <attr name="lineWidth" format="dimension" />
     <!--每节虚线的宽-->
    <attr name="imaginary_width" format="dimension"/>
    <!--每节虚线的间隔-->
    <attr name="interval_width" format="dimension"/>
  </declare-styleable>

OK 到此结束。代码很简单,就没写注释~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,802评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,032评论 19 139
  • 翻译自“Collection View Programming Guide for iOS” 0 关于iOS集合视...
    lakerszhy阅读 3,930评论 1 22
  • 桃花 一笑 墙外桃花宜销愁, 何寻烦恼满腹馊。 世间寂寥平常事, 一支独放享自由。
    看云阁阅读 333评论 0 1
  • 到健身房运动时,你有发现别人什么坏习惯吗?在Huffpost Healthy Living网站上分享了10个破坏你...
    林烁nathan阅读 365评论 0 1