如何正确比较坐标?
how to correctly compare coordinates?
我有带有绘制文本和用户绘制线条的自定义视图。
文本有一些 (x,y) 坐标,线条也有(一旦用户绘制它)。我只是想比较一下,那条线的坐标和文本的坐标是一样的。
但由于很难用手指敲出精确的坐标,所以无法直接比较给用户看,他好像连接了2个文本,但坐标略有不同。
如何正确操作?
这是我的直接比较代码(方法isCorrectConnection
):
public class FingerLineView extends View {
private final Paint mPaint;
private final Paint mPaintText;
private float startX, startY;
private float endX, endY;
public FingerLineView(Context context) {
this(context, null);
}
public FingerLineView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.RED);
mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintText.setColor(Color.BLACK);
mPaintText.setTextSize(30);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(startX, startY, endX, endY, mPaint);
canvas.drawText("TEXT", 10, 200, mPaintText);
canvas.drawText("TEXT", 200, 200, mPaintText);
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
// Set the end to prevent initial jump
endX = event.getX();
endY = event.getY();
invalidate();
break;
case MotionEvent.ACTION_MOVE:
endX = event.getX();
endY = event.getY();
invalidate();
break;
case MotionEvent.ACTION_UP:
endX = event.getX();
endY = event.getY();
isCorrectConnection(startX, startY, endX, endY, 10, 200, 200, 200);
invalidate();
break;
}
return true;
}
public boolean isCorrectConnection(float startX, float startY, float endX, float endY,
float textX1, float textY1, float textX2, float textY2) {
return startX == textX1 && startY == textY1 && endX == textX2 && endY == textY2;
}
}
根据我对你的问题的理解,你想检查用户是否点击了第一个文本并移动(拖动)到第二个文本,反之亦然,你应该检查按下整个文本而不仅仅是左上角绘制的文本,所以我有一个答案,这两个文本应该不同,因为知道有像素大小,然后检查这个文本矩形内的触摸,而不仅仅是我描述的左上角点,所以让我们开始吧:首先你应该将您的 isCorrectConnection
函数改为:-
private boolean isCorrectConnection(float startX, float startY, float endX, float endY,
float x1, float y1, float x2, float y2
String text1, String text2) {
//Measuring text1's size
Rect textBounds = new Rect();
mPaint.getTextBounds(text1, 0, text1.length(), textBounds);
int w1 = textBounds.width();
int h1 = textBounds.height();
//Measuring text2's size
Rect textBounds2 = new Rect();
mPaint.getTextBounds(text2, 0, text2.length(), textBounds2);
int w2 = textBounds2.width();
int h2 = textBounds2.height();
//Checking for touched and moved from text1 to text2
if(startX >= x1 && startX <= (x1 + w1) && startY >= y1 && startY <= (y1 + h1) &&
endX >= x2 && endX <= (x2 + w2) && endY >= y2 && endY <= (y2 + h2))
return true;
//Checking for touched and moved from text2 to text1
if(startX >= x2 && startX <= (x2 + w2) && startY >= y2 && startY <= (y2 + h2) &&
endX >= x1 && endX <= (x1 + w1) && endY >= y1 && endY <= (y1 + h1))
return true;
return false;
}
如果您确定文本相同,请改为执行以下操作:-
private boolean isCorrectConnection(float startX, float startY, float endX, float endY,
float x1, float y1, float x2, float y2
String text) {
//Measuring text's size
Rect textBounds = new Rect();
mPaint.getTextBounds(text, 0, text.length(), textBounds);
int w1 = textBounds.width(), w2 = w1;
int h1 = textBounds.height(), h2 = h1;
//Checking for touched and moved from text1 to text2
if(startX >= x1 && startX <= (x1 + w1) && startY >= y1 && startY <= (y1 + h1) &&
endX >= x2 && endX <= (x2 + w2) && endY >= y2 && endY <= (y2 + h2))
return true;
//Checking for touched and moved from text2 to text1
if(startX >= x2 && startX <= (x2 + w2) && startY >= y2 && startY <= (y2 + h2) &&
endX >= x1 && endX <= (x1 + w1) && endY >= y1 && endY <= (y1 + h1))
return true;
return false;
}
我有带有绘制文本和用户绘制线条的自定义视图。
文本有一些 (x,y) 坐标,线条也有(一旦用户绘制它)。我只是想比较一下,那条线的坐标和文本的坐标是一样的。
但由于很难用手指敲出精确的坐标,所以无法直接比较给用户看,他好像连接了2个文本,但坐标略有不同。
如何正确操作?
这是我的直接比较代码(方法isCorrectConnection
):
public class FingerLineView extends View {
private final Paint mPaint;
private final Paint mPaintText;
private float startX, startY;
private float endX, endY;
public FingerLineView(Context context) {
this(context, null);
}
public FingerLineView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.RED);
mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintText.setColor(Color.BLACK);
mPaintText.setTextSize(30);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(startX, startY, endX, endY, mPaint);
canvas.drawText("TEXT", 10, 200, mPaintText);
canvas.drawText("TEXT", 200, 200, mPaintText);
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
// Set the end to prevent initial jump
endX = event.getX();
endY = event.getY();
invalidate();
break;
case MotionEvent.ACTION_MOVE:
endX = event.getX();
endY = event.getY();
invalidate();
break;
case MotionEvent.ACTION_UP:
endX = event.getX();
endY = event.getY();
isCorrectConnection(startX, startY, endX, endY, 10, 200, 200, 200);
invalidate();
break;
}
return true;
}
public boolean isCorrectConnection(float startX, float startY, float endX, float endY,
float textX1, float textY1, float textX2, float textY2) {
return startX == textX1 && startY == textY1 && endX == textX2 && endY == textY2;
}
}
根据我对你的问题的理解,你想检查用户是否点击了第一个文本并移动(拖动)到第二个文本,反之亦然,你应该检查按下整个文本而不仅仅是左上角绘制的文本,所以我有一个答案,这两个文本应该不同,因为知道有像素大小,然后检查这个文本矩形内的触摸,而不仅仅是我描述的左上角点,所以让我们开始吧:首先你应该将您的 isCorrectConnection
函数改为:-
private boolean isCorrectConnection(float startX, float startY, float endX, float endY,
float x1, float y1, float x2, float y2
String text1, String text2) {
//Measuring text1's size
Rect textBounds = new Rect();
mPaint.getTextBounds(text1, 0, text1.length(), textBounds);
int w1 = textBounds.width();
int h1 = textBounds.height();
//Measuring text2's size
Rect textBounds2 = new Rect();
mPaint.getTextBounds(text2, 0, text2.length(), textBounds2);
int w2 = textBounds2.width();
int h2 = textBounds2.height();
//Checking for touched and moved from text1 to text2
if(startX >= x1 && startX <= (x1 + w1) && startY >= y1 && startY <= (y1 + h1) &&
endX >= x2 && endX <= (x2 + w2) && endY >= y2 && endY <= (y2 + h2))
return true;
//Checking for touched and moved from text2 to text1
if(startX >= x2 && startX <= (x2 + w2) && startY >= y2 && startY <= (y2 + h2) &&
endX >= x1 && endX <= (x1 + w1) && endY >= y1 && endY <= (y1 + h1))
return true;
return false;
}
如果您确定文本相同,请改为执行以下操作:-
private boolean isCorrectConnection(float startX, float startY, float endX, float endY,
float x1, float y1, float x2, float y2
String text) {
//Measuring text's size
Rect textBounds = new Rect();
mPaint.getTextBounds(text, 0, text.length(), textBounds);
int w1 = textBounds.width(), w2 = w1;
int h1 = textBounds.height(), h2 = h1;
//Checking for touched and moved from text1 to text2
if(startX >= x1 && startX <= (x1 + w1) && startY >= y1 && startY <= (y1 + h1) &&
endX >= x2 && endX <= (x2 + w2) && endY >= y2 && endY <= (y2 + h2))
return true;
//Checking for touched and moved from text2 to text1
if(startX >= x2 && startX <= (x2 + w2) && startY >= y2 && startY <= (y2 + h2) &&
endX >= x1 && endX <= (x1 + w1) && endY >= y1 && endY <= (y1 + h1))
return true;
return false;
}