所以我想做的是:使用OnTouchListener,当它拖动到某个位置(另一个ImageView)时,我必须将ImageView捕捉到Android中的适当位置。拖动工作正常,获取第二个 ImageView 的位置很好,只是当我尝试设置其位置时,它总是在该位置下对齐。如何在不使用 mImageView - 5 的情况下将其对齐到第二个 ImageView 上方,因为这样它就无法在每个具有或多或少像素的 Android 屏幕上工作。这是代码
int[] locXY = new int[2];
mImageView5.getLocationOnScreen(locXY);
int x1 = locXY[0];
int y1 = locXY[1];
int[] img3 = new int[2];
mImageView2.getLocationOnScreen(locXY);
int x2 = locXY[0];
int y2 = locXY[1];
//if you reach the locating of imageview2 then snap into place
if (x1 <= x2 + 15 &&
y1 <= y2 + 20 &&
y1 >= y2 - 20 &&
x1 >= x2 - 15) {
//do this
tts.speak("You dont go there", TextToSpeech.QUEUE_FLUSH, null);
mImageView5.setX(x2);
mImageView3.setTop(y2 + 5); //this should be -5 because it will make it go up, but i dont want to use a - or a +
mImageView5.setBottom(mImageView3.getTop());
} // end if
最佳答案
好吧,所以这可能不是最好的方法,但我所做的是我有一个预设的 ImageView ,它的 Y 坐标与我想要该 ImageView 去的位置相同。所以我做了这样的事情
int[] locXY = new int[2];
mImageView5.getLocationOnScreen(locXY);
int x1 = locXY[0];
int y1 = locXY[1];
int[] img2 = new int[2];
mImageView2.getLocationOnScreen(img2);
int x2 = img2[0];
int y2 = img2[1];
int[] img3 = new int[2];
mImageView3.getLocationOnScreen(img3);
int x3 = img3[0];
int y3 = img3[1];
int[] img4 = new int[2];
mImageView16.getLocationOnScreen(img4);
int x4 = img4[0];
int y4 = img4[1];
Log.d("X and Y", "X and Y of imageview 5" + x2 + " " + x2+" " +y1+ " " + y2);
if ( x1 <= x2 + 15 && mImageView5.isEnabled() != false &&
y1 <= y2 + 20 &&
y1 >= y2 - 20 &&
x1 >= x2 - 15
)
{
//do this
tts.speak("That looks correct", TextToSpeech.QUEUE_FLUSH, null);
mImageView5.forceLayout();
mImageView5.setX(x2);
mImageView5.setY(y4);
mImageView5.setEnabled(false);
mImageView5.bringToFront();
}
这将使 imageview 16 的 Y 坐标与我想要它去的位置的 x 坐标相匹配。我解决了这个问题,但没有真正回答如何做的问题,只是回答了如何解决。我会保留一两天,看看是否有人能找到更好的东西
关于java - 如何将 android ImageView 捕捉到屏幕上的某个位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21568473/