2017-08-06 38 views
0

我仍然不可思議的新的android編程(像3周大),但我慢慢地得到的東西的hang。。我已經在不同的網站上尋找答案,但我還沒有找到任何答案。保持邊界內的按鈕

我的Java代碼至今(至少什麼是相關的):

view = (ImageView)findViewById(R.id.imageView3); 
backgroundImageName = String.valueOf(view.getTag()); 

view.setOnTouchListener(new View.OnTouchListener() { 
    public boolean onTouch(View v, MotionEvent event) { 
     int myNewX = (int)event.getX(); //this line and the next get the X & Y coordinates of wherever the mouse clicked 
     int myNewY = (int)event.getY(); 
     Button button = (Button)findViewById(R.id.add_text); 

     // find a way to keep the button within the borders of the white square 
     if (event.getAction() == MotionEvent.ACTION_UP) { //checks if the mouse click was released 
      button.setVisibility(View.VISIBLE); 
      button.setX(myNewX - 160); //this line and the next set the coordinates of the button (plus the adjustment) 
      button.setY(myNewY + 70); //to make the button by above and in the middle of where the mouse clicked 
     } 
     else 
     { 
      button.setVisibility(View.INVISIBLE); 
     } 
     return true; 
    } 
}); 

和我的XML代碼至今(至少什麼是相關的):

<ImageView 
    android:id="@+id/imageView3" 
    android:layout_width="450dp" 
    android:layout_height="450dp" 
    android:layout_marginBottom="8dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginTop="8dp" 
    android:contentDescription="@string/white_background" 
    android:tag="white" 
    android:visibility="invisible" 
    app:layout_constraintBottom_toBottomOf="@+id/button" 
    app:layout_constraintLeft_toLeftOf="@+id/imageView2" 
    app:layout_constraintRight_toRightOf="@+id/imageView2" 
    app:layout_constraintTop_toTopOf="@+id/textView" 
    app:srcCompat="@mipmap/white" /> 

<Button 
    android:id="@+id/add_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/add_text" 
    android:visibility="invisible" 
    tools:ignore="MissingConstraints" 
    tools:layout_editor_absoluteY="71dp" 
    tools:layout_editor_absoluteX="16dp" /> 

我想要做的是保持按鈕在圖片中顯示的邊界內,但我不知道如何做到這一點。我得到了它是如何工作的這是一個想法:

If mouseclick (the event) is outside the border { set the x coordinate of the button to the edge of the border and the same for the y coordinate }

我一直在試圖讓它正常工作,但它只是拒絕。如果任何人都可以幫我弄清楚我需要的代碼,我真的很感謝幫助。

謝謝

borders

回答

1

首先創建一個邊界檢查,然後執行你的任務:

view.setOnTouchListener(new View.OnTouchListener() { 
public boolean onTouch(View v, MotionEvent event) { 
    //obtain the boundaries of the view 
    if(event.getAction() == MotionEvent.ACTION_DOWN){ 
    rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); 

    } 
    if (event.getAction() == MotionEvent.ACTION_UP){ 
     //Catches out of boundary user's movement. 
     if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + 
     (int) event.getY())){ 

     button.setVisibility(View.VISIBLE); 
     //perform your other calculations 

    } 

    return true; 
} 

});

注意:從API 14可以使用:Android的MotionEvent的ACTION_HOVER_ENTER: https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER