在android中相对于屏幕宽度缩放图像

Iul*_*urt 12 android image scale android-layout

我有两个图像的布局:

  • 一个应该拉伸到屏幕宽度
  • 一个上面应该缩放到相同比例的第一个被自动缩放(相对于原始图像大小)

更具体:两个图像是同一图像的切片,因此它们内部的一些细节应该匹配.
我可以用XML制作吗?

如果我不能通过XML来做,也许我可以预先缩放图形.在这种情况下,我应该如何预先缩放它们?

Mat*_*lis 6

这有点像黑客,但它允许你在xml中执行此操作.

如果您知道,例如,顶部图像是底部图像大小的X%,那么您可以使用LinearLayout的layout_weight来根据屏幕的百分比来定位和调整顶部图像的大小:

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView android:id="@+id/left_filler" android:layout_weight="20"
        android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    <ImageView android:id="@+id/top_image" android:layout_weight="50"
        android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    <ImageView android:id="@+id/right_filler" android:layout_weight="30"
        android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
 ... bottom image
Run Code Online (Sandbox Code Playgroud)

上面的内容会将top_image的大小设置为屏幕的50%,偏离左边20%.只要top_image的大小是bottom_image的50%,这将保持相似的比例.

或者,执行此操作的"正确"方法可能是在自定义视图中覆盖onDraw()并使用画布绘制方法.