2018-04-26

public class LineViewextends View {

private static final float StartX =100;

private static final float StartY =700;

private static final float textSize =40;

private Paintpaint;

private Pathpath;

public LineView(Context context) {

super(context);

}

public LineView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

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

super(context, attrs, defStyleAttr);

init();

}

private void init() {

paint =new Paint();

paint.setColor(Color.RED);

paint.setStrokeWidth(5);

paint.setStyle(Paint.Style.STROKE);

paint.setTextSize(textSize);

path =new Path();

path.moveTo(StartX,StartY);

}

@Override

    protected void onDraw(Canvas canvas) {

//      canvas.drawLine(50,50,250,250,paint);

        canvas.drawPath(path,paint);

//画Y轴

        canvas.drawLine(StartX,StartY,StartX,100,paint);

//画X轴

        canvas.drawLine(StartX,StartY,700,StartY,paint);

canvas.drawText("0",100-paint.measureText("0")-20,700+textSize,paint);

for (int i =100; i <=700; i+=100) {

String text = i+"";

float textWidth =paint.measureText(text);

canvas.drawText(text,StartX+i-textWidth/2,StartY+textSize,paint);//画x坐标

            canvas.drawText(text,StartX-textWidth,StartY-i+textSize/2,paint);//画y坐标

        }

}

public void addPoi(int x,int y) {

path.lineTo(StartX+x,StartY-y);

invalidate();

}

}

你可能感兴趣的:(2018-04-26)