Android在ControintLayout中制作一个可调整大小的分割屏幕
我想在ContrintLayout中有一个拆分器,以便能够更改2个图像视图的相对大小 示例:拖动下面的双箭头时,应根据箭头位置调整image1/image2的大小 这是我的布局Android在ControintLayout中制作一个可调整大小的分割屏幕,android,android-constraintlayout,Android,Android Constraintlayout,我想在ContrintLayout中有一个拆分器,以便能够更改2个图像视图的相对大小 示例:拖动下面的双箭头时,应根据箭头位置调整image1/image2的大小 这是我的布局 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<?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>
在开始时,我的拆分器图像位于图像之间,但当我移动它时,imageView1和imageView2上的约束不会更新,也不会调整其大小,并且我的拆分器图像在屏幕上独立于imageView1和imageView2移动
这就是我得到的
这就是我想要的:
我怎样才能做到这一点呢?在尝试了更多之后,我发现了一个使用指南的问题: 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>
我希望它能帮助别人
<?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>
@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;
}