Android 进入相对年轻的安卓系统

Android 进入相对年轻的安卓系统,android,android-relativelayout,Android,Android Relativelayout,我正在制作一个应用程序,其中一个RelativeLayout完全适合屏幕。我还添加了一个按钮,可以使用setScaleX()ansetScaleY()在布局中放大此布局。因此(显然)整个布局不再适合屏幕。因此,我首先想到使用滚动视图可以在相对视图中移动,但这不起作用。有什么建议吗?提前谢谢 以下是xml代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="h

我正在制作一个应用程序,其中一个
RelativeLayout
完全适合屏幕。我还添加了一个按钮,可以使用
setScaleX()
an
setScaleY()
在布局中放大此布局。因此(显然)整个布局不再适合屏幕。因此,我首先想到使用
滚动视图
可以在
相对视图
中移动,但这不起作用。有什么建议吗?提前谢谢

以下是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;
        }
    }
});
final View grid=findviewbyd(R.id.grid);
最终点dispSize=新点();
getWindowManager().getDefaultDisplay().getSize(dispSize);
setOnTouchListener(新的OnTouchListener(){
私有点f mAnchor=新点f();
私有点f mTouchPoint=新点f();
@凌驾
公共布尔onTouch(视图、运动事件、运动事件){
mTouchPoint.set(motionEvent.getX(),motionEvent.getY());
开关(motionEvent.getActionMasked()){
case MotionEvent.ACTION\u DOWN:
mAnchor.set(mTouchPoint);
返回true;
case MotionEvent.ACTION\u移动:
float dx=mTouchPoint.x-mAnchor.x;
float dy=mTouchPoint.y-mAnchor.y;
左浮点=grid.getX()+dx;
float right=left+grid.getWidth();
float top=grid.getY()+dy;
float bottom=top+grid.getHeight();
if(grid.getWidth()>dispSize.x){
如果(左>0f){
左=0f;
}
如果(右dispSize.y){
如果(顶部>0f){
top=0f;
}
如果(底部

注意,这个OnTouchListener将只接收RelativeLayout的内容视图未截获的触摸事件(我们在XML代码中没有看到)。这意味着内容视图需要通过在其自己的触摸/手势侦听器中返回false来向上传递任何未使用的触摸事件

给我看看你的xml代码。谢谢你的回答,但我仍然有一个问题。它的移动量没有限制。我可以将布局拖出屏幕。你有什么解决办法吗?感谢最初的回答只是想让听众知道你能做些什么。我对答案进行了编辑,以显示一种将网格保持在屏幕限制范围内的简单方法。您的具体需求可能需要进一步改进,但希望您能理解。