作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Android 编程仍然是令人难以置信的新手(大约 3 周大),但我正在慢慢掌握窍门。我在不同的网站上寻找答案,但还没有找到任何东西。
到目前为止我的 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" />
我想做的是将按钮保持在图片所示的边界内,但我不知道该怎么做。我知道它应该如何工作:
如果 mouseclick(事件)在边框之外 {
将按钮的 x 坐标设置为边框边缘
y 坐标也是如此
}
我一直试图让它正常工作,但它就是拒绝。如果有人可以帮助我找出使其工作所需的代码,我将非常感谢您的帮助。
谢谢
最佳答案
First create a boundary check and then perform your task:
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;
}
});
Note: From API 14 you can use: Android MotionEvent's ACTION_HOVER_ENTER: https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER
关于java - 将按钮保持在边界内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45528741/
我是一名优秀的程序员,十分优秀!