如何将Android ImageView捕捉到屏幕上的某个位置?

时间:2014-02-05 04:10:04

标签: java android android-layout android-imageview

所以我要做的是:使用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

1 个答案:

答案 0 :(得分:0)

好的,所以这可能不是最好的方法,但我所做的是我有一个预设的imageview,它具有与我想要这个imageview相同的Y坐标。所以我做了类似的事情

            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坐标相匹配。我解决了它,但没有真正回答如何做的问题,只是回答了如何解决。我将开放一两天,看看是否有人能找到更好的东西