在 RelativeLayout 中移动 android
Moving in RelativeLayout android
我正在制作一个 RelativeLayout
完全适合屏幕的应用程序。我已经添加了一个按钮,该按钮使用 setScaleX()
和 setScaleY()
来放大此布局。结果(很明显)整个布局不再适合屏幕。所以我首先想到使用 ScrollView
可以在 RelativeLayout
中移动,但这没有用。有什么建议么?提前致谢。
这里是 xml 代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="phou.minesweeper.GameActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/chronometer"
android:layout_below="@+id/textViewBest">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true">
<RelativeLayout
android:id="@+id/grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"></RelativeLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
我正在缩放的 RelativeLayout
是 id = grid 的那个。
xml 代码很短,因为所有视图都是在 java.
中创建的
当您使用 setScaleX/Y 在 Java 中操纵视图大小时,您可能需要在 RelativeLayout 上实现一个 onTouchListener 来处理拖动事件,以允许您以编程方式移动整个布局。
假设您缩放的视图是@id/grid,一个简单的 OnTouchListener 看起来像这样。
final View grid = findViewById(R.id.grid);
final Point dispSize = new Point();
getWindowManager().getDefaultDisplay().getSize(dispSize);
grid.setOnTouchListener(new OnTouchListener() {
private PointF mAnchor = new PointF();
private PointF mTouchPoint = new PointF();
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mTouchPoint.set(motionEvent.getX(), motionEvent.getY());
switch (motionEvent.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mAnchor.set(mTouchPoint);
return true;
case MotionEvent.ACTION_MOVE:
float dx = mTouchPoint.x - mAnchor.x;
float dy = mTouchPoint.y - mAnchor.y;
float left = grid.getX() + dx;
float right = left + grid.getWidth();
float top = grid.getY() + dy;
float bottom = top + grid.getHeight();
if (grid.getWidth() > dispSize.x) {
if (left > 0f) {
left = 0f;
}
if (right < dispSize.x) {
left += dispSize.x - right;
}
}
if (grid.getHeight() > dispSize.y) {
if (top > 0f) {
top = 0f;
}
if (bottom < dispSize.y) {
top += dispSize.y - bottom;
}
}
grid.setX(left);
grid.setY(top);
return true;
default:
return true;
}
}
});
请注意,此 OnTouchListener 将仅接收未被 RelativeLayout 的内容视图拦截的触摸事件(我们在 XML 代码中看不到)。这意味着内容视图需要通过在它们自己的 touch/gesture 侦听器中返回 false 来将任何未使用的触摸事件向上传递到层次结构中。
我正在制作一个 RelativeLayout
完全适合屏幕的应用程序。我已经添加了一个按钮,该按钮使用 setScaleX()
和 setScaleY()
来放大此布局。结果(很明显)整个布局不再适合屏幕。所以我首先想到使用 ScrollView
可以在 RelativeLayout
中移动,但这没有用。有什么建议么?提前致谢。
这里是 xml 代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="phou.minesweeper.GameActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/chronometer"
android:layout_below="@+id/textViewBest">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true">
<RelativeLayout
android:id="@+id/grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"></RelativeLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
我正在缩放的 RelativeLayout
是 id = grid 的那个。
xml 代码很短,因为所有视图都是在 java.
当您使用 setScaleX/Y 在 Java 中操纵视图大小时,您可能需要在 RelativeLayout 上实现一个 onTouchListener 来处理拖动事件,以允许您以编程方式移动整个布局。
假设您缩放的视图是@id/grid,一个简单的 OnTouchListener 看起来像这样。
final View grid = findViewById(R.id.grid);
final Point dispSize = new Point();
getWindowManager().getDefaultDisplay().getSize(dispSize);
grid.setOnTouchListener(new OnTouchListener() {
private PointF mAnchor = new PointF();
private PointF mTouchPoint = new PointF();
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mTouchPoint.set(motionEvent.getX(), motionEvent.getY());
switch (motionEvent.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mAnchor.set(mTouchPoint);
return true;
case MotionEvent.ACTION_MOVE:
float dx = mTouchPoint.x - mAnchor.x;
float dy = mTouchPoint.y - mAnchor.y;
float left = grid.getX() + dx;
float right = left + grid.getWidth();
float top = grid.getY() + dy;
float bottom = top + grid.getHeight();
if (grid.getWidth() > dispSize.x) {
if (left > 0f) {
left = 0f;
}
if (right < dispSize.x) {
left += dispSize.x - right;
}
}
if (grid.getHeight() > dispSize.y) {
if (top > 0f) {
top = 0f;
}
if (bottom < dispSize.y) {
top += dispSize.y - bottom;
}
}
grid.setX(left);
grid.setY(top);
return true;
default:
return true;
}
}
});
请注意,此 OnTouchListener 将仅接收未被 RelativeLayout 的内容视图拦截的触摸事件(我们在 XML 代码中看不到)。这意味着内容视图需要通过在它们自己的 touch/gesture 侦听器中返回 false 来将任何未使用的触摸事件向上传递到层次结构中。