在Android中,我将ImageView
的layout_width
定义为fill_parent
(占用手机的整个宽度)。
如果我放到ImageView
的图像大于layout_width
,Android会缩放它,对吧? 但身高呢? 当Android缩放图像时,它会保持纵横比吗?
我发现当Android缩放比ImageView
更大的图像时, ImageView
的顶部和底部会有一些空白区域。 真的吗? 如果是,我该如何消除那个空白区?
#1楼
如果您希望图像占据最大可能空间,那么最佳选择就是
android:layout_weight="1"
android:scaleType="fitCenter"
#2楼
我有一个小于屏幕的图像。 为了让它按比例拉伸到最大值并在视图中居中,我必须使用以下代码:
<ImageView
android:id="@+id/my_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitCenter" />
但请注意,如果您有相对布局并且某些元素设置在ImageView的上方或下方,则它们很可能会与图像重叠。
#3楼
对于任何想要使图像与图像视图完全匹配且具有适当缩放并且不使用裁剪的人
imageView.setScaleType(ScaleType.FIT_XY);
其中imageView是表示ImageView的视图
#4楼
这对我有用:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="39dip"
android:scaleType="centerCrop"
android:adjustViewBounds ="true"
#5楼
我用这个:
<ImageView
android:id="@+id/logo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="centerInside"
android:src="@drawable/logo" />
#6楼
myImageView.setAdjustViewBounds(true);
#7楼
尝试使用android:layout_gravity
for ImageView:
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
上面的例子对我有用。
#8楼
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 130, 110, false));
#9楼
我有一个算法可以将位图缩放到bestFit容器尺寸,保持其纵横比。 请在这里找到我的解决方案
希望这有助于有人走下车道!
#10楼
看看ImageView.ScaleType控制和理解调整大小的方式发生在一个ImageView
。 调整图像大小(同时保持其宽高比)时,图像的高度或宽度可能会小于ImageView
的尺寸。
#11楼
是的,默认情况下,Android会缩小图像以适应ImageView,从而保持纵横比。 但是,请确保使用
android:src="..."
而不是android:background="..."
将图像设置为ImageView。src=
使其缩放图像维持宽高比,但background=
使其缩放并扭曲图像,使其完全适合ImageView的大小。 (您可以同时使用背景和源,这对于仅使用一个ImageView在主图像周围显示框架等内容非常有用。)您还应该看到
android:adjustViewBounds
以使ImageView调整自身大小以适应重新缩放的图像。 例如,如果在通常为正方形的ImageView中有一个矩形图像,adjustViewBounds = true将使ImageView的大小也变为矩形。 这会影响其他视图在ImageView周围的布局方式。然后,正如Samuh所写,您可以使用
android:scaleType
参数更改默认缩放图像的方式。 顺便说一下,发现它是如何工作的最简单方法就是自己试验一下! 请记住查看模拟器本身(或实际手机)中的布局,因为Eclipse中的预览通常是错误的。
#12楼
下面的代码工作比例图像作为宽高比:
Bitmap bitmapImage = BitmapFactory.decodeFile("Your path");
int nh = (int) ( bitmapImage.getHeight() * (512.0 / bitmapImage.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(bitmapImage, 512, nh, true);
your_imageview.setImageBitmap(scaled);
#13楼
您可以计算屏幕宽度。 你可以缩放位图。
public static float getScreenWidth(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float pxWidth = outMetrics.widthPixels;
return pxWidth;
}
按屏幕宽度计算屏幕宽度和缩放图像高度。
float screenWidth=getScreenWidth(act)
float newHeight = screenWidth;
if (bitmap.getWidth() != 0 && bitmap.getHeight() != 0) {
newHeight = (screenWidth * bitmap.getHeight()) / bitmap.getWidth();
}
您可以缩放位图。
Bitmap scaledBitmap=Bitmap.createScaledBitmap(bitmap, (int) screenWidth, (int) newHeight, true);
#14楼
如果希望ImageView调整其边界以保持其drawable的纵横比,请将此属性设置为true。
#15楼
通过您的ImageView并根据您可以制作的屏幕高度和宽度
public void setScaleImage(EventAssetValueListenerView view){
// Get the ImageView and its bitmap
Drawable drawing = view.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float xScale = ((float) 4) / width;
float yScale = ((float) 4) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
width = scaledBitmap.getWidth();
height = scaledBitmap.getHeight();
view.setImageDrawable(result);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
#16楼
在ImageView中使用这些属性以保持纵横比:
android:adjustViewBounds="true"
android:scaleType="fitXY"
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
#17楼
哟不需要任何java代码。 你只需要:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
关键是宽度和高度的匹配父级
#18楼
以编程方式执行此操作时,请确保以正确的顺序调用setter:
imageView.setAdjustViewBounds(true)
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP)
#19楼
这解决了我的问题
android:adjustViewBounds="true"
android:scaleType="fitXY"
#20楼
如果使用cardview
来舍入imageview
并修复android:layout_height
for header这对我来说是用Glide
加载图像的
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="220dp"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
card_view:cardBackgroundColor="@color/colorPrimary"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="10dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true">
<ImageView
android:adjustViewBounds="true"
android:maxHeight="220dp"
android:id="@+id/iv_full"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"/>
</android.support.v7.widget.CardView>
</FrameLayout>
#21楼
如果图像质量下降:使用
android:adjustViewBounds="true"
代替
android:adjustViewBounds="true"
android:scaleType="fitXY"
#22楼
您可以缩放图像,这也会缩小图像的大小。 有一个库,您可以下载并使用它。 https://github.com/niraj124124/Images-Files-scale-and-compress.git
使用方法1)导入压缩机-v1.0。 jar到你的项目。 2)添加以下示例代码进行测试。 ResizeLimiter resize = ImageResizer.build(); resize.scale(“inputImagePath”,“outputImagePath”,imageWidth,imageHeight); 根据您的要求,还有更多方法可供选择
#23楼
这是它在ConstraintLayout中对我有用的方式:
<ImageView
android:id="@+id/myImg"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"/>
然后在代码中,我将drawable设置为:
ImageView imgView = findViewById(R.id.myImg);
imgView.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.image_to_show, null));
这可以很好地根据其纵横比拟合图像并使其保持在中心位置。
#24楼
对于有这个特殊问题的其他人。 您有一个想要具有fill_parent
宽度和高度按比例缩放的ImageView
(或其他View
):
将这两个属性添加到ImageView
:
android:adjustViewBounds="true"
android:scaleType="centerCrop"
并将ImageView
宽度设置为fill_parent
,将height设置为wrap_content
。
此外,如果您不希望裁剪图像,请尝试以下操作:
android:adjustViewBounds="true"
android:layout_centerInParent="true"
#25楼
如果您希望ImageView
在保持适当的宽高比的同时向上和向下缩放,请将其添加到XML:
android:adjustViewBounds="true"
android:scaleType="fitCenter"
将其添加到您的代码中:
// We need to adjust the height if the width of the bitmap is
// smaller than the view width, otherwise the image will be boxed.
final double viewWidthToBitmapWidthRatio = (double)image.getWidth() / (double)bitmap.getWidth();
image.getLayoutParams().height = (int) (bitmap.getHeight() * viewWidthToBitmapWidthRatio);
我需要一段时间才能完成这项工作,但这似乎适用于图像小于屏幕宽度且大于屏幕宽度的情况,并且它不会对图像进行包装。