我想下载一个图像(大小未知,但总是大致正方形)并显示它以使其水平填充屏幕,并垂直拉伸以保持图像的宽高比,在任何屏幕尺寸上.这是我的(非工作)代码.它水平拉伸图像,但不垂直,因此被压扁...
ImageView mainImageView = new ImageView(context);
mainImageView.setImageBitmap(mainImage); //downloaded from server
mainImageView.setScaleType(ScaleType.FIT_XY);
//mainImageView.setAdjustViewBounds(true);
//with this line enabled, just scales image down
addView(mainImageView,new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Run Code Online (Sandbox Code Playgroud) 每当我将背景分配给使用wrap_content布局的视图时,如果背景图像大于视图,则视图会被拉伸,以便它可以保持整个背景.为什么会如此以及如何预防呢?即使图像是9-patch并且标记了可伸展区域,也会出现这种情况 - 为什么图像缩小到视图的大小?
我有一个drawable,需要作为背景.它需要拉伸才能填满屏幕(忽略纵横比).
我为ldpi,mdpi,hdpi和xhdpi提供了合适的大小.(我知道它们与相应的宽度和高度的比例是合适的,这还够吗?)
我试过这样做如下:
在imageView上使用scaleType:fitXY
这确实在imageView周围(内部?)留下了白色边距.

我不满意的解决方法是使用scaleType中心,如下所示:
我不满意,因为平板电脑屏幕上仍然存在这个问题.

注意:
更新1:
这是XML布局
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" -->
<!-- xmlns:tools="http://schemas.android.com/tools" -->
<!-- android:id="@+id/FrameLayout1" -->
<!-- android:layout_width="fill_parent" -->
<!-- android:layout_height="fill_parent" -->
<!-- tools:context=".SplashScreen" > -->
<!--
This FrameLayout insets its children based on system windows using
android:fitsSystemWindows
-->
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="onClickTapToContinue"
>
</View>
<ImageView
android:id="@+id/bg_gradient"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="fill"
android:adjustViewBounds="true"
android:contentDescription="@string/this_is_the_background_image"
android:scaleType="center"
android:src="@drawable/bg_gradient" />
<ImageView
android:id="@+id/bg_artwork"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/artwork_man"
android:src="@drawable/bg_artwork" />
<!-- </FrameLayout> -->
<ImageView …Run Code Online (Sandbox Code Playgroud)