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

来源:互联网 发布:淘宝摇一摇 编辑:程序博客网 时间:2024/05/18 03:01
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);    }}
原创粉丝点击