**我如何在画布上用文字画线。显示的文字是在画布上绘制时用线显示的线的长度。 请帮助我解决这种情况。
这是我尝试过的输出图像。 电流输出: ** Geolocation
这是我尝试过的工程图视图代码。
class Line {
float startX, startY, stopX, stopY;
public Line(float startX, float startY, float stopX, float stopY) {
this.startX = startX;
this.startY = startY;
this.stopX = stopX;
this.stopY = stopY;
}
public Line(float startX, float startY) { // for convenience
this(startX, startY, startX, startY);
}
}
public class DrawView extends View {
Paint paint = new Paint();
Paint paint1 = new Paint();
ArrayList<Line> lines = new ArrayList<Line>();
Path path = new Path();
private void init() {
paint.setColor(Color.BLACK);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeJoin(Paint.Join.MITER);
}
private void drawArrow1(float startX, float startY, float stopX, float stopY, Canvas canvas, Paint paint1) {
double degree = calculateDegree(startX, stopX, startY, stopY);
float endX1 = (float) (stopX + ((20) * Math.cos(Math.toRadians((degree-30)+90))));
float endY1 = (float) (stopY + ((20) * Math.sin(Math.toRadians(((degree-30)+90)))));
float endX2 = (float) (stopX + ((20) * Math.cos(Math.toRadians((degree-60)+180))));
float endY2 = (float) (stopY + ((20) * Math.sin(Math.toRadians(((degree-60)+180)))));
canvas.drawLine(stopX,stopY,endX1,endY1,paint1);
canvas.drawLine(stopX, stopY, endX2,endY2,paint1);
}
private void drawArrow(float startX, float startY, float stopX, float stopY, Canvas canvas, Paint paint1) {
double degree1 = calculateDegree(stopX, startX, stopY, startY);
float endX11 = (float) (startX + ((20) * Math.cos(Math.toRadians((degree1-30)+90))));
float endY11 = (float) (startY + ((20) * Math.sin(Math.toRadians(((degree1-30)+90)))));
float endX22 = (float) (startX + ((20) * Math.cos(Math.toRadians((degree1-60)+180))));
float endY22 = (float) (startY + ((20) * Math.sin(Math.toRadians(((degree1-60)+180)))));
canvas.drawLine(startX,startY,endX11,endY11,paint1);
canvas.drawLine(startX,startY,endX22,endY22,paint1);
}
public double calculateDegree(float x1, float x2, float y1, float y2) {
float startRadians = (float) Math.atan((y2 - y1) / (x2 - x1));
System.out.println("radian=====" + Math.toDegrees(startRadians));
startRadians += ((x2 >= x1) ? 90 : -90) * Math.PI / 180;
return Math.toDegrees(startRadians);
}
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Override
public void onDraw(Canvas canvas) {
for (Line l : lines) {
canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY, paint);
drawArrow(l.startX, l.startY, l.stopX, l.stopY,canvas,paint);
drawArrow1(l.startX, l.startY, l.stopX, l.stopY,canvas,paint);
}
}
float first_x, first_y, last_x, last_y, line_lenght;
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
first_x = x;
first_y = y;
lines.add(new Line(event.getX(), event.getY()));
return true;
}
else if ((event.getAction() == MotionEvent.ACTION_MOVE ||
event.getAction() == MotionEvent.ACTION_UP) &&
lines.size() > 0) {
Line current = lines.get(lines.size() - 1);
current.stopX = event.getX();
current.stopY = event.getY();
last_x = x;
last_y = y;
line_lenght = (float) Math.pow(Math.abs(first_x-last_x),2) + (float) Math.pow(Math.abs(first_y-last_y),2);
line_lenght = (float) Math.sqrt(line_lenght);
Log.i("lenght", String.valueOf(line_lenght));
invalidate();
return true;
}
else {
return false;
}
}
}
答案 0 :(得分:0)
我有自己问题的一点解决方案。这是我得到的解决方案。
class Line {
float startX, startY, stopX, stopY;
public Line(float startX, float startY, float stopX, float stopY) {
this.startX = startX;
this.startY = startY;
this.stopX = stopX;
this.stopY = stopY;
}
public Line(float startX, float startY) { // for convenience
this(startX, startY, startX, startY);
}
}
public class DrawView extends View {
Paint paint = new Paint();
ArrayList<Line> lines = new ArrayList<Line>();
TextPaint textPaint = new TextPaint();
private void init() {
paint.setColor(Color.BLUE);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeJoin(Paint.Join.MITER);
textPaint.setAntiAlias(true);
textPaint.setTypeface(Typeface.create("Arial", Typeface.BOLD));
textPaint.setTextSize(18 * getResources().getDisplayMetrics().density);
textPaint.setColor(Color.BLACK);
}
private void drawArrow1(float startX, float startY, float stopX, float stopY, Canvas canvas, Paint paint1) {
double degree = calculateDegree(startX, stopX, startY, stopY, canvas);
float line_lenghtt = (float) Math.pow(Math.abs(startX-stopX),2) + (float) Math.pow(Math.abs(startY-stopY),2);
line_lenghtt = (float) Math.sqrt(line_lenghtt);
float inch = (float) (line_lenghtt / 95.999999998601);
float m = (startX+stopX)/2;
float m1 = (startY+stopY)/2;
canvas.drawText(String.format("%.2f", inch)+"″",m,m1,textPaint);
float endX1 = (float) (stopX + ((20) * Math.cos(Math.toRadians((degree-30)+90))));
float endY1 = (float) (stopY + ((20) * Math.sin(Math.toRadians(((degree-30)+90)))));
float endX2 = (float) (stopX + ((20) * Math.cos(Math.toRadians((degree-60)+180))));
float endY2 = (float) (stopY + ((20) * Math.sin(Math.toRadians(((degree-60)+180)))));
canvas.drawLine(stopX,stopY,endX1,endY1,paint1);
canvas.drawLine(stopX, stopY, endX2,endY2,paint1);
}
private void drawArrow(float startX, float startY, float stopX, float stopY, Canvas canvas, Paint paint1) {
double degree1 = calculateDegree(stopX, startX, stopY, startY,canvas);
float endX11 = (float) (startX + ((20) * Math.cos(Math.toRadians((degree1-30)+90))));
float endY11 = (float) (startY + ((20) * Math.sin(Math.toRadians(((degree1-30)+90)))));
float endX22 = (float) (startX + ((20) * Math.cos(Math.toRadians((degree1-60)+180))));
float endY22 = (float) (startY + ((20) * Math.sin(Math.toRadians(((degree1-60)+180)))));
canvas.drawLine(startX,startY,endX11,endY11,paint1);
canvas.drawLine(startX,startY,endX22,endY22,paint1);
}
public double calculateDegree(float x1, float x2, float y1, float y2, Canvas canvas) {
float startRadians = (float) Math.atan((y2 - y1) / (x2 - x1));
System.out.println("radian=====" + Math.toDegrees(startRadians));
startRadians += ((x2 >= x1) ? 90 : -90) * Math.PI / 180;
return Math.toDegrees(startRadians);
}
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Override
public void onDraw(Canvas canvas) {
for (Line l : lines) {
canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY, paint);
drawArrow(l.startX, l.startY, l.stopX, l.stopY,canvas,paint);
drawArrow1(l.startX, l.startY, l.stopX, l.stopY,canvas,paint);
}
}
float first_x, first_y, last_x, last_y, line_lenght;
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
first_x = x;
first_y = y;
lines.add(new Line(event.getX(), event.getY()));
return true;
}
else if ((event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_UP) &&
lines.size() > 0) {
Line current = lines.get(lines.size() - 1);
current.stopX = event.getX();
current.stopY = event.getY();
last_x = x;
last_y = y;
line_lenght = (float) Math.pow(Math.abs(first_x-last_x),2) + (float) Math.pow(Math.abs(first_y-last_y),2);
line_lenght = (float) Math.sqrt(line_lenght);
Log.i("lenght", String.valueOf(line_lenght));
invalidate();
return true;
}
else {
return false;
}
}
}