Android开发:相同配置下TouchImageView横向图片缩放异常问题求助
看起来你的问题主要出在布局约束错误和ImageView属性组合冲突上,结合TouchImageView的特性,我给你整理了以下解决方案:
1. 修复分隔线的布局约束
首先看你的分隔线editor_divider的约束,它被设置为app:layout_constraintEnd_toEndOf="parent",这意味着它会贴在容器的最右侧。这样一来,第一个ImageVieweditor_first_image的宽度会被拉伸到整个容器(从parent start到divider start,也就是parent end),而第二个ImageVieweditor_second_image的宽度会变成0(从divider end到parent end,两者位置重合)。这显然不是你想要的左右分栏效果。
把分隔线的约束修改为居中,让它把容器分成左右均等的两部分:
<View android:id="@+id/editor_divider" android:layout_width="1dp" android:layout_height="match_parent" android:background="@color/black" android:visibility="invisible" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/>
2. 调整TouchImageView的属性组合
你当前设置的adjustViewBounds="true"和scaleType="center"会和TouchImageView的缩放逻辑冲突:
adjustViewBounds="true"会让ImageView根据图片宽高比调整自身尺寸,但在ConstraintLayout的0dp宽度约束下,这个属性会干扰约束布局的宽度计算,导致图片显示异常。scaleType="center"只会让图片居中显示,不会缩放适配View大小,超出View的部分会被裁剪,完全发挥不了TouchImageView的缩放移动能力。
修改两个TouchImageView的属性,移除adjustViewBounds,并更换更合适的scaleType:
<com.ortiz.touchview.TouchImageView android:id="@+id/editor_first_image" android:layout_width="0dp" android:layout_height="match_parent" android:scaleType="centerInside" android:background="@color/mainGreen" app:layout_constraintEnd_toStartOf="@+id/editor_divider" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent"/> <com.ortiz.touchview.TouchImageView android:id="@+id/editor_second_image" android:layout_width="0dp" android:layout_height="match_parent" android:scaleType="centerInside" android:background="@color/gray_toolbar" app:layout_constraintStart_toEndOf="@id/editor_divider" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"/>
centerInside会让图片保持宽高比,缩放到能完全显示在View内的最大尺寸,同时居中,之后你就可以正常使用TouchImageView的触摸缩放和移动功能了。
3. 检查代码中的动态设置
最后,确认你没有在代码中给editor_first_image设置额外的缩放限制,比如调用setMaxZoom()设置了过小的缩放值,或者动态修改了它的布局参数,这些都可能导致显示异常。
内容的提问来源于stack exchange,提问作者Serhii Bilotserkovets

