根据我对您的问题的理解,如果您想检查用户是否单击第一个文本并移动(拖动)到第二个文本,或者反之亦然,您应该检查是否按下整个文本,而不仅仅是绘制文本的左上角,所以我有一个答案给你,这两个文本的应该是不同的,因为知道有像素大小,然后在这个文本矩形内检查触摸,而不是像我所描述的左上角点,所以让我们开始:首先你应该改变你的
isCorrectConnection
此函数instead:-
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;
}