···自定义的view
public class ZheView extends View {
private Paint mPaint;
private final static int textSize = 40;
private final static int startX = 100;
private final static int startY = 800;
private Path mPath;
public ZheView(Context context) {
super(context);
}
public ZheView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ZheView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.RED);
mPaint.setTextSize(textSize);
mPath = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(mPath,mPaint);
canvas.drawLine(startX,startY,800,startY,mPaint);
canvas.drawLine(startX,startY,startX,100,mPaint);
canvas.drawText("0",100-mPaint.measureText("0")-20,800+textSize,mPaint);
for (int i = 100; i < 600; i+=100) {
String text = i+"";
float textWidth = mPaint.measureText(text);
canvas.drawText(text,startX+i-textWidth/2,startY+textSize,mPaint);//画x坐标
canvas.drawText(text,startX-textWidth,startY-i+textSize,mPaint);//画y坐标
// canvas.drawLine(startX,200,300,400,mPaint);
// canvas.drawLine(startX,300,300,500,mPaint);
}
}
public void addPoi(int x,int y ){
mPath.lineTo(startX+x,startY-y);
invalidate();
}
}
···在界面显示的
int arrx[] = {100,140,200,260,300,400};
int arry[] = {30,100,300,210,500,310};
for(int i =0; i<6; i++){
zheView.addPoi(arrx[i],arrx[i]);
}