Android 缩放和重叠图像视图
我有点像android noob,已经为这个问题挣扎太久了 我的目标是: 两个图像视图。第一个将是屏幕的宽度,并绘制出与src图像的比例相匹配的图像。第二个ImageView需要缩放,以便其宽度是第一个图像的0到1之间的固定倍数。 我最终需要能够触摸并拖动第二个较小的图像到第一个固定图像的顶部,尽管这部分不是这个问题的重点 这通常都是通过编程实现的,因为src映像和比例倍数直到运行时才知道 我尝试过的是:我可以使用alignParentTop等将两个图像视图叠加在一个RelativeLayout中。这很好,但我看不出在这种情况下如何缩放图像 我也可以使用LinearLayout缩放两个图像视图,方法是将它们的布局权重分别设置为1和缩放倍数。在这种情况下,我无法使图像重叠,但这不是严格意义上的真实情况-我已经用负边距处理了这一点,但这是一个非常粗糙的解决方案,将使以后几乎不可能实现顶部图像的移动 所以我的问题是:有没有办法解决我尝试实施的两种解决方案中的任何一种?或者有没有更好的方法来解决这个问题?Android 缩放和重叠图像视图,android,imageview,android-linearlayout,android-relativelayout,Android,Imageview,Android Linearlayout,Android Relativelayout,我有点像android noob,已经为这个问题挣扎太久了 我的目标是: 两个图像视图。第一个将是屏幕的宽度,并绘制出与src图像的比例相匹配的图像。第二个ImageView需要缩放,以便其宽度是第一个图像的0到1之间的固定倍数。 我最终需要能够触摸并拖动第二个较小的图像到第一个固定图像的顶部,尽管这部分不是这个问题的重点 这通常都是通过编程实现的,因为src映像和比例倍数直到运行时才知道 我尝试过的是:我可以使用alignParentTop等将两个图像视图叠加在一个RelativeLayout
我不一定要在代码中寻找答案,只是我应该采用的方法 可以使用约束布局 将第一个ImageView放置在所需位置 对你的父母有限制 将第二个ImageView放置在所需位置 具有对父视图的约束,而不具有任何其他子视图 方法是,如果将子视图放置在ConstraintLayout中,并将约束设置为父视图,则可以在不影响其他视图的情况下操纵缩放/平移 要在运行时更改视图的位置,可以使用 view.setTranslationX和view.setTranslationY 还可以通过编程方式创建视图,并对父视图进行约束 我个人的喜好 可以使用此选项在ConstraintLayout中覆盖不同的视图
<ImageView
android:id="imageID1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="yourImageStable"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
// put your own constraints-
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ImageView>
<ImageView
android:id="imageID2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="yourImageUnStable"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
Note: This two constraint value will draw an ImageView from the Top-Left
corner of the Parent View (on Position x=0, y=0)
Remember: the View which you want to overlay should be written next to the other,
like here ImageID2 can overlay ImageID1.
</ImageView>
请说得更准确些