自定义View之LineView(线条View)

2020-08-08  本文已影响0人  撩人的风光

这个View目前包含  直线  虚线  波浪线,下面我把View的代码粘上了

/*

* Created by dell

* on 2020/8/7

* The package is sport.randong.com.randongsport.view

* This Class is ...

*/

public class LineViewextends View {

private float lineSize =0;//线粗细

    private float lineWidth =0;//线宽

    private float lineHeight =0;//线高

    private int lineColor =0;//线的颜色

    private int lineAngle =0;//线的偏折角度

    private int lineStyle =0;//线的风格

    private int orientation =0;//线的方向  有横向纵向两个取值范围

    private int lineSmallLength =0;//虚线的 每个小横线的长度

    private int lineSmallSpace =0;//虚线的 每个小横线之间的间距

    private float lineWavyCrestSpace =0;//波峰间距

    private float lineWavyCrestHeight =0;//波峰高度

//--------------------------------线的风格取值--------------------------------

    public static final int LINE_STRAIGNT =1;//直线

    public static final int LINE_IMAGINARY =2;//虚线

    public static final int LINE_WAVY =3;//波浪线

//------------------------------线的方向取值范围------------------------------

    public static final int VERTICAL =1;//竖向

    public static final int HORIZONTAL =2;//横向

    private Paintpaint;

public LineView(Context context,@Nullable AttributeSet attrs) {

this(context, attrs,0);

}

public LineView(Context context,@Nullable AttributeSet attrs,int defStyleAttr) {

super(context, attrs, defStyleAttr);

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LineView);

lineSize = ta.getDimension(R.styleable.LineView_line_size,0);

lineHeight = ta.getDimension(R.styleable.LineView_line_height,0);

lineWidth = ta.getDimension(R.styleable.LineView_line_width,0);

lineColor = ta.getColor(R.styleable.LineView_line_color,0x00000000);//线的颜色  默认黑色

        lineAngle = ta.getInt(R.styleable.LineView_line_angle,0);//线的偏折角度  默认0度(12点中方向)

        lineStyle = ta.getInt(R.styleable.LineView_line_style,0);

orientation = ta.getInt(R.styleable.LineView_orientation,1);//线的方向  默认纵向

        lineSmallLength = ta.getInt(R.styleable.LineView_line_small_length,5);//虚线的 每个小横线的长度  默认5

        lineSmallSpace = ta.getInt(R.styleable.LineView_line_small_space,5);//虚线的 每个小横线之间的间距  默认5

        lineWavyCrestHeight = ta.getDimension(R.styleable.LineView_line_wavy_crest_height,10);//波峰高度

        lineWavyCrestSpace = ta.getDimension(R.styleable.LineView_line_wavy_crest_space,10);//波峰间距

        paint =new Paint();

paint.setColor(lineColor);

paint.setStrokeWidth(lineSize *2);

}

@Override

    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {

int specModeWidth = MeasureSpec.getMode(widthMeasureSpec);

int specModeHeight = MeasureSpec.getMode(heightMeasureSpec);

int specSizwWidth = MeasureSpec.getSize(widthMeasureSpec);

int specSizwHeight = MeasureSpec.getSize(heightMeasureSpec);

//        switch (specModeWidth) {

//            case MeasureSpec.EXACTLY:

//            case MeasureSpec.UNSPECIFIED: {

//                //铺满

//                lineWidth = specSizwWidth;

//                lineHeight = lineSize;

//            }

//            break;

//            case MeasureSpec.AT_MOST: {

//                //自适应

//            }

//            break;

//        }

//        switch (specModeHeight) {

//            case MeasureSpec.EXACTLY:

//            case MeasureSpec.UNSPECIFIED: {

//                //铺满

//                lineHeight = specSizwHeight;

//                lineWidth = lineSize;

//            }

//            break;

//            case MeasureSpec.AT_MOST: {

//                //自适应

//            }

//            break;

//        }

        if (lineStyle ==LINE_WAVY) {

//这里是波浪线的测量方法

            if (orientation ==VERTICAL) {

if (specModeHeight == MeasureSpec.EXACTLY) {

lineHeight = specSizwHeight;

}

setMeasuredDimension((int)lineWavyCrestHeight +500, (int)lineHeight);

}else if (orientation ==HORIZONTAL) {

if (specModeWidth == MeasureSpec.EXACTLY) {

lineWidth = specSizwWidth;

}

setMeasuredDimension((int)lineWidth, (int)lineWavyCrestHeight);

}

return;

}

if (orientation ==VERTICAL) {

if (specModeHeight == MeasureSpec.EXACTLY) {

lineHeight = specSizwHeight;

}

lineWidth =lineSize;

}else if (orientation ==HORIZONTAL) {

if (specModeWidth == MeasureSpec.EXACTLY) {

lineWidth = specSizwWidth;

}

lineHeight =lineSize;

}else {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

setMeasuredDimension((int)lineWidth, (int)lineHeight);

}

@Override

    protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

if (lineStyle ==LINE_STRAIGNT)

if (orientation ==VERTICAL) {

//竖线

                canvas.drawLine(0,0,0,lineHeight,paint);

}else {

//横线

                canvas.drawLine(0,0,lineWidth,0,paint);

}

else if (lineStyle ==LINE_IMAGINARY) {

//虚线

            int width = getWidth();//View的宽度

            int height = getHeight();//View的高度

            if (orientation ==VERTICAL) {

//竖线

                for (int index =0; index <= height; index +=lineSmallLength +lineSmallSpace) {

canvas.drawLine(0, index,0, index +lineSmallLength,paint);

}

}else {

//横线

                for (int index =0; index <= width; index +=lineSmallLength +lineSmallSpace) {

canvas.drawLine(index,0, index +lineSmallLength,0,paint);

}

}

}else if (lineStyle ==LINE_WAVY) {

paint.setStyle(Paint.Style.STROKE);

paint.setAntiAlias(true);

//波浪线

            int height = getHeight();

int width = getWidth();

Path path =new Path();

if (orientation ==VERTICAL) {

//竖线

                for (int i =0; i < height; i +=lineWavyCrestSpace *2) {

path.moveTo(width /2, i);

path.quadTo(width /2 -lineWavyCrestHeight, (i +lineWavyCrestSpace - (lineWavyCrestSpace /2)), width /2, i +lineWavyCrestSpace);

path.moveTo(width /2, i +lineWavyCrestSpace);

path.quadTo(lineWavyCrestHeight + width /2, (i +lineWavyCrestSpace *2 - (lineWavyCrestSpace /2)), width /2, i +lineWavyCrestSpace *2);

canvas.drawPath(path,paint);

}

}else if (orientation ==HORIZONTAL) {

//横线

                for (int i =0; i < width; i +=lineWavyCrestSpace *2) {

path.moveTo(i, height /2);

path.quadTo((i +lineWavyCrestSpace - (lineWavyCrestSpace /2)), height /2 -lineWavyCrestHeight, i +lineWavyCrestSpace, height /2);

path.moveTo(i +lineWavyCrestSpace, height /2);

path.quadTo((i +lineWavyCrestSpace *2 - (lineWavyCrestSpace /2)),lineWavyCrestHeight + height /2, i +lineWavyCrestSpace *2, height /2);

canvas.drawPath(path,paint);

}

}

}

}

}

这个就是View的源码,可能性能问题没有考虑。。。接下来是自定义属性

<declare-styleable name="LineView">

<attr name="line_size" format="dimension" />

<!--线的粗细-->

<attr name="line_width" format="dimension" /><!--线宽-->

<attr name="line_height" format="dimension" /><!--线高-->

<attr name="line_color" format="color" /><!--线的颜色-->

<attr name="line_angle" format="integer" /><!--线偏折角度 0度为12点 默认为0-->

<attr name="line_style">

<enum name="straignt" value="1" /><!--直线-->

<enum name="imaginary" value="2" /><!--虚线-->

<enum name="wavy" value="3" /><!--波浪线-->

</attr>

<attr name="orientation"><!--线的方向-->

<enum name="vertical" value="1" /> <!--纵向-->

<enum name="horizontal" value="2" /><!--横向-->

</attr>

<!--虚线属性配置-->

<attr name="line_small_length" format="integer" /><!--虚线的 小横线的长度-->

<attr name="line_small_space" format="integer" /><!--虚线之间的间距-->

<!--波浪线特有属性-->

<attr name="line_wavy_crest_space" format="dimension" /><!--波峰间距-->

<attr name="line_wavy_crest_height" format="dimension" /><!--波峰高度-->

</declare-styleable>

之后再添加新的线条进去  目前这些可以满足基本使用

上一篇 下一篇

猜你喜欢

热点阅读