android - 如何在画布上用线条绘制文本?
问题描述
**如何在画布上用文字画线。显示的文本是在画布上绘制时以线条显示的线条长度。请帮助我解决这种情况。
这是我尝试过的绘图视图代码。
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;
}
}
}
解决方案
我对自己的问题有了一点解决方案。这是我得到的解决方案。
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;
}
}
}
推荐阅读
- visual-studio-2017 - Visual Studio 中的 VSTS 提要“刷新”机制
- javascript - Angularjs-在角度变量中使用 url 作为嵌入式 youtube src
- angularjs - 在 HTML 中显示 angularJS 范围元素
- apache-spark - Pyspark 按位置推断时区
- html - 新闻模板,如菜单布局
- pandas - csv 文件中的字符串和引号
- javascript - 什么是 Google Optimize 中 Javascript 变量定位中的全局变量名称?
- php - 如何在 ubuntu 终端本地检查 php 脚本设置?
- r - 在调用 RcmdrPlugin.BCA 时,我收到此错误消息:找不到对象“importRODBCtable”
- javascript - 是什么阻止了这个网页缩小到小于 530 像素的宽度?