gpt4 book ai didi

Android 在约束布局内制作可调整大小的分屏

转载 作者:行者123 更新时间:2023-12-04 12:36:11 25 4
gpt4 key购买 nike

我想在 constraintlayout 中有一个拆分器,以便能够更改 2 个 ImageView 的相对大小。

示例:拖动下面的双箭头时,应根据箭头位置调整 image1/image2 的大小。

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

我怎样才能做到这一点?

最佳答案

在尝试了更多之后,我发现了一个使用指南的解决方案:

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;

}

我希望它可以帮助某人。

关于Android 在约束布局内制作可调整大小的分屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61521373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com