classMyDraw {

Paint p;

Canvas canvas;unit_y = 10;preInit_x = 50;preInit_y = 550;//最大的Y顶点。

Point origin;MyDraw() {super();

p= new Paint(); //创建画笔

origin= new Point(preInit_x, preInit_y);//坐标系的原点

}/*** 拖动的范围,x > 0说明是 从左到右拖动。 x<0是从右向左拖动

*

*@paramx_changedonFliing(floatx_changed) {float newX = origin.x +x_changed;if (newX >preInit_x)

newX=preInit_x;int minNewX = -((result.length) * unit_x -surfaceView1.getWidth());(newX

newX=minNewX;

isToEnd= true;

}int x = (int) newX;if (x ==origin.x)return;

origin= new Point(x, origin.y);//更改坐标系原点的位置

draw();if (isToEnd) {//触发 到达顶点的方法。

raiseScrollToEnd();

}

}public voiddraw() {

canvas=surfaceHolder.lockCanvas();

onDdraw(canvas);

surfaceHolder.unlockCanvasAndPost(canvas);

}public voidonDdraw(Canvas c) {

Log.i("PDWY", String.format("新原点位置 :(%s, %s)", origin.x, origin.y));

Rect r;int height =c.getHeight();int width =c.getWidth();

c.drawColor(Color.BLACK);

p.setColor(Color.RED);

p.setStrokeWidth(2);

p.setStyle(Paint.Style.STROKE);

r= new Rect(2, 2, width - 2, height - 2);

c.drawRect(r, p);

p.reset();

p.setColor(Color.RED);

p.setStrokeWidth(5);float[] lines = new float[max_unit];

lines=evalPosition(result, unit_x, unit_y, origin);//lines = new float[]{0,0,50,500,100,400,100,400,150,500,0,0};//drawLines方法用一组float表示要绘制的直线,每个直线用4个点表示,前两个为起端点,后两个为终端点

c.drawLines(lines, 2, lines.length - 2, p);

p.reset();

p.setColor(Color.parseColor("#dcdcdc"));

drawEndPoint(lines,2, lines.length - 2, p, c);

}private void drawEndPoint(float[] lines, int offset, intcount,

Paint p2, Canvas c) {for (int i = offset; i < count; i += 2) {float x =lines[i];float y = lines[i + 1];

c.drawCircle(x, y,8, p2);

}

}private float[] evalPosition(float[] result2, intunit_widht,intunit_height, Point origin) {if (result2 == null)[0];float[] val = new float[result2.length * 4];for (int i = 0; i < result2.length; i++) {float y = origin.y - result2[i] *unit_height;float x = origin.x + unit_widht *i;

val[i* 4 + 0] =x;

val[i* 4 + 1] =y;

val[i* 4 + 2] =x;

val[i* 4 + 3] =y;

}returnval;

}final int max_unit = 6;public voidonSurfaceChanged() {

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐