安卓系统在一个反转布局里面做一个可调整大小的分屏。

问题描述 投票:1回答:1

我想在一个contraintlayout里面有一个分割器,能够改变2个图像视图的相对大小。

例如:当拖动下面的双箭头时,应该根据箭头位置调整图像1图像2的大小。

Screen splitter

对于这一点,这里是我的布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">


    <ImageView
        android:id="@+id/image1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_divider_img" />


    <ImageView
        android:id="@+id/image2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/main_divider_img"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <ImageView
        android:id="@+id/main_divider_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/divider" />



</androidx.constraintlayout.widget.ConstraintLayout>

并且在我的分割器图像上有一个onTouchListener。

 View.OnTouchListener dividerTouchListener = new View.OnTouchListener() {

        private float  mInitialY;


        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    mInitialY = view.getY() - motionEvent.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    view.animate().y(motionEvent.getRawY() + mInitialY).setDuration(0).start()                  
                    break;
                default:
                    return false;
            }
            return true;

        }
    };

在开始时,我的分割器图像在图像之间,但当我移动它时,imageView1和imageView2的约束没有更新,也没有调整大小,我的分割器图像在屏幕上的移动独立于imageView1和imageView2。

这是我得到的结果

Result

这就是我想要的。

Expected

我怎样才能实现这个功能?

android android-constraintlayout
1个回答
0
投票

经过更多的尝试,我找到了一个使用引导线的somution。

现在imageView和splitview都受制于引导线。

当移动分割图像时,我改变了准则百分比

这里是布局。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/image1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:background="@color/rejectedRed"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <ImageView
        android:id="@+id/image2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/acceptedGreen"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/image1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.41" />

    <ImageView
        android:id="@+id/main_divider_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        app:srcCompat="@drawable/divider" />


</androidx.constraintlayout.widget.ConstraintLayout>

这里是touchListener简单多了。

 @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            int height = 0;
            switch (motionEvent.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_MOVE:
                    height = ((ConstraintLayout) findViewById(R.id.main)).getHeight();
                    Guideline guideLine = (Guideline) findViewById(R.id.guideline);
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) guideLine.getLayoutParams();
                    params.guidePercent = (motionEvent.getRawY()) / height;
                    guideLine.setLayoutParams(params);

                    break;
                default:
                    return false;
            }
            return true;

        }

我希望它能帮助某人。

© www.soinside.com 2019 - 2024. All rights reserved.