ImageView具有各种XML属性以缩放其内容,各种布局视图允许放置视图并设置其大小。
但是,在某些情况下,我无法弄清楚如何很好地缩放imageView。
它的一个示例是将ImageView放在(例如,frameLayout的)底部,尽可能地缩放其宽度,并仍然保持长宽比(不裁剪)。
我发现没有一种组合有效,包括各种ScaleType值,adjustViewBounds,重力,...
似乎ImageView缺少一些重要的ScaleType值。
到目前为止,对我而言唯一有效的解决方案是将imageView设置为底部(使用将重力设置为底部和水平中心),并使用类似于以下内容的代码:
final ImageView logoBackgroundImageView = ...;
final LayoutParams layoutParams = logoBackgroundImageView.getLayoutParams();
final Options bitmapOptions = ImageService.getBitmapOptions(getResources(), R.drawable.splash_logo);
final int screenWidth = getResources().getDisplayMetrics().widthPixels;
layoutParams.height = bitmapOptions.outHeight * screenWidth / bitmapOptions.outWidth;
logoBackgroundImageView.setLayoutParams(layoutParams);
这通常可以正常工作,并且在不以全屏显示的情况下也不需要太多更改就可以正常工作,但是我想知道是否有更简单的方法。
万一高度过大,它可能不起作用,因此您可能需要更改它,以便在这种情况下将宽度设置为较小,以使所有内容都适合容器。
是否有解决与ImageView相关的各种问题的解决方案或库?
编辑:这是我这次想做的示例图像,在一个垂直的LinearLayout内,它在两个其他视图之间的中间界定了ImageView的位置:
如您所见,在某些(或全部?)预览中,中间图像向右对齐,而不是停留在中间(水平)上。
我希望它占用所有获得的空间并进行缩放(保持宽高比),但要使其与空间的底部对齐。
这是我尝试过的一种组合的示例XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF339AE2">
<View
android:layout_width="match_parent"
android:id="@+id/topView"
android:layout_alignParentTop="true"
android:layout_height="100dp"
android:background="#FF00ff00"
/>
<FrameLayout
android:layout_below="@+id/topView"
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:layout_above="@+id/bottomView"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:background="#FFffff00"
android:layout_gravity="bottom|center_horizontal"
android:src="@android:drawable/sym_def_app_icon"
android:scaleType="fitEnd"
android:layout_height="match_parent"
/>
</FrameLayout>
<View
android:background="#FFff0000"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/bottomView"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</RelativeLayout>
同样,请注意,我已经尝试了多种组合,但是它们都不起作用,因此,如果您建议对XML进行更改,请先尝试一下。另请注意,以上内容是一个POC(以便于阅读)。
顺便说一句,我建议的解决方法适用于某些(或大多数?)情况,但不是全部。您可以通过旋转设备来注意到它。也许有一种方法可以修复它,或者从ImageView扩展它来提供额外的保护。
您需要三个属性的组合:
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
该adjustViewBounds
属性是必需的,因为ImageView
默认情况下会测量自身以匹配可绘制对象的原始尺寸,而不考虑根据比例类型执行的任何比例缩放。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句