Android 自定义View(二),点,线的绘制

public class PointLine extends View {

    Paint mLinePaint;

    Paint mPointPaint;

    float width;
    float height;

    float pointAddress[] = new float[]{72, 200,  216, 200, 360, 200,  504, 200, 648, 200};
    float lineAddress[] = new float[]{72, 200, 216, 200, 216, 200, 360, 200, 360, 200, 504, 200, 504, 200, 648, 200};

    public PointLine(Context context) {
        this(context, null);
    }

    public PointLine(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PointLine(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mLinePaint = new Paint();
        mLinePaint.setColor(Color.RED);
        mPointPaint = new Paint();
        mPointPaint.setColor(Color.BLACK);


    }


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

        width = getMeasuredWidth() / 5;
        height = getMeasuredHeight() / 2;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //一定要设置线的的宽度
        mPointPaint.setStrokeWidth((float) 20.0);         //线宽
        //点的数组一定是2的整数倍
        canvas.drawPoints(pointAddress, mPointPaint);

        //一定要设置线的的宽度
        mLinePaint.setStrokeWidth((float) 10.0);
        //线的数组一定是4的整数倍
        canvas.drawLines(lineAddress, mLinePaint);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值