Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确比较坐标?_Java_Android_Android Canvas_Coordinate Systems - Fatal编程技术网

Java 如何正确比较坐标?

Java 如何正确比较坐标?,java,android,android-canvas,coordinate-systems,Java,Android,Android Canvas,Coordinate Systems,我有绘制文本和用户绘制线条的自定义视图 文本有一些(x,y)坐标,直线也有(只要用户绘制)。我只是想比较一下,直线坐标和文本坐标是一样的 但因为很难用手指精确地找到坐标,所以不可能直接对用户进行比较。看起来他连接了两个文本,但坐标略有不同 我怎样才能正确地做呢 这是我的直接比较代码(方法isCorrectConnection): 根据我对您的问题的理解,您希望检查用户是否单击第一个文本并移动(拖动)到第二个文本,或者反之亦然,您应该检查是否按下整个文本,而不仅仅是按下绘制文本的左上角,因此,我有

我有绘制文本和用户绘制线条的自定义视图

文本有一些(x,y)坐标,直线也有(只要用户绘制)。我只是想比较一下,直线坐标和文本坐标是一样的

但因为很难用手指精确地找到坐标,所以不可能直接对用户进行比较。看起来他连接了两个文本,但坐标略有不同

我怎样才能正确地做呢

这是我的直接比较代码(方法
isCorrectConnection
):


根据我对您的问题的理解,您希望检查用户是否单击第一个文本并移动(拖动)到第二个文本,或者反之亦然,您应该检查是否按下整个文本,而不仅仅是按下绘制文本的左上角,因此,我有一个答案,这两个文本的大小应该不同,以像素为单位,然后检查文本矩形内的触摸,而不仅仅是我所描述的左上角,因此让我们开始:首先,您应该将
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;
}
private boolean isCorrectConnection(float-startX、float-startY、float-endX、float-endY、,
浮动x1、浮动y1、浮动x2、浮动y2
字符串text1,字符串text2){
//测量text1的大小
Rect textBounds=new Rect();
mPaint.getTextBounds(text1,0,text1.length(),textBounds);
int w1=textBounds.width();
int h1=textBounds.height();
//测量text2的大小
Rect textbunds2=新的Rect();
mPaint.getTextBounds(text2,0,text2.length(),textBounds2);
int w2=textBounds2.width();
int h2=textBounds2.height();
//检查是否触摸并从text1移动到text2

如果(startX>=x1&&startX=y1&&startY=x2&&endX=y2&&endY=x2&&startX=y2&&startY=x1&&endX=y1&&endY=x1&&startX=y1&&startY=x2&&endX=y2&&startY=x1&&endX=y1&&endY)您可以声明特定区域,因此,每当用户触及该区域线中的任何位置时,都将从您的坐标开始nt@AadityaBrahmbhatt你能给我举个例子吗?对我来说,这就像是一个巨大的检查:
if(0
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;
}