自定义viewAndroid知识Android开发经验谈

Android 简单实现 虚线View

2017-06-12  本文已影响174人  大崔527

在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 到此结束。代码很简单,就没写注释~

上一篇下一篇

猜你喜欢

热点阅读