Android ImageView:适合宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12059328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ImageView: Fit Width
提问by Sharj
I download image and set it as a screen background dynamically using Imageview
. I have tried ScaleType
, to scale the image.
我下载图像并将其动态设置为屏幕背景Imageview
。我试过ScaleType
,缩放图像。
If image height is larger than width then ScaleTypes
fitStart
, fitEnd
and fitCenter
don't work. Android scale down the photo and fit it based on the height, but I see some extra blank space as part of the width.
如果图像高度大于宽度,则大ScaleTypes
fitStart
,fitEnd
并且fitCenter
不工作。Android 缩小照片并根据高度调整它,但我看到一些额外的空白空间作为宽度的一部分。
I want to scale down the photo based on the width so that it fits the width and I don't care if there's some extra blank space as part of the height or if height is too long it is fine if it's going out of the view(if that's possible?).
我想根据宽度缩小照片,使其适合宽度,我不在乎是否有一些额外的空白作为高度的一部分,或者如果高度太长,如果它不在视图中就好了(如果可能的话?)。
ScaleType.XY
scale the photo and fit everything in the ImageView
and doesn't care about image height/weight ratio.
ScaleType.XY
缩放照片并将所有内容放入其中ImageView
,并且不关心图像的高度/重量比。
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitStart"
/>
回答by Sharj
I ended up using this code:
我最终使用了这个代码:
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/name"
/>
Make sure you set the image using src
instead of background
.
确保您使用src
而不是 来设置图像background
。
回答by ViliusK
android:adjustViewBounds="true"
does the job!
android:adjustViewBounds="true"
做的工作!
回答by Michel-F. Portzert
This elegant solution found herewill work like a charm for you.
在这里找到的这个优雅的解决方案对您来说就像一个魅力。
Basically you just have to create a small class that extends ImageView
and simply override the onMeasure
method to adjust the width and height as you want. Here it scales to fit width by using:
基本上,您只需要创建一个小类来扩展ImageView
并简单地覆盖该onMeasure
方法以根据需要调整宽度和高度。在这里,它使用以下方法缩放以适应宽度:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
setMeasuredDimension(width, height);
}
You would use this special ImageView
like this:
你会ImageView
像这样使用这个特殊的:
<your.activity.package.AspectRatioImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:src="@drawable/test" />
回答by Dan Harvey
There is a simple method if you simply want to fill the width of screen and have height proportional (and know the ratio of the image) you can do this in OnCreate():
有一个简单的方法,如果您只是想填充屏幕的宽度并使其高度成比例(并知道图像的比例),您可以在 OnCreate() 中执行此操作:
setContentView(R.layout.truppview_activity);
trupImage = (ImageView) findViewById(R.id.trupImage);
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float scWidth = outMetrics.widthPixels;
trupImage.getLayoutParams().width = (int) scWidth;
trupImage.getLayoutParams().height = (int) (scWidth * 0.6f);
Where 0.6 is the ratio adjustment (you could also calc automatically of course if you know the w & h of image).
其中 0.6 是比例调整(当然,如果您知道图像的 w & h,您也可以自动计算)。
PS:
Here is the XML side:
PS:
这是XML方面:
<ImageView
android:id="@+id/trupImage"
android:layout_width="match_parent"
android:background="@color/orange"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
回答by Thomas VJ
This works for me.
这对我有用。
Create a class extends ImageView
创建一个类扩展 ImageView
package com.yourdomain.utils;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d != null) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
In xml use the following instead of ImageView
在 xml 中使用以下而不是 ImageView
<com.yourdomain.utils.ResizableImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/test" />
回答by Guillaume Alouege
I think you cant do it only with XML, you need to resize yourself, the bitmap
我认为你不能只用 XML 来做,你需要调整自己的大小,位图
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
try {
((ImageView) findViewById(R.id.background))
.setImageBitmap(ShrinkBitmap(width));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
then
然后
private Bitmap ShrinkBitmap(int width)
throws FileNotFoundException {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.img, bmpFactoryOptions);
int widthRatio = (int) android.util.FloatMath
.ceil(bmpFactoryOptions.outWidth / (float) width);
bmpFactoryOptions.inSampleSize = widthRatio;
if (bmpFactoryOptions.inSampleSize <= 0)
bmpFactoryOptions.inSampleSize = 0;
bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.img, bmpFactoryOptions);
return bitmap;
}
And the layout
和布局
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
回答by SK. Fuad
if you want to fit the image whole parent block
it will stretch the image
如果你想适合整个父块
的图像,它会拉伸图像
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
And if you want to fit image whole parent with original ratio of image
it will crop out some part of image
如果你想用图像的原始比例来适应整个父级,
它会裁剪出图像的一部分
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"