Android 调整 ImageView 的大小以适应纵横比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12400113/
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
Resizing ImageView to fit to aspect ratio
提问by Muz
I need to resize an ImageView such that it fits to the screen, maintains the same aspect ratio. The following conditions hold:
我需要调整 ImageView 的大小,使其适合屏幕,并保持相同的纵横比。以下条件成立:
- Images are a fixed width (size of the width of screen)
- Images are downloaded from the Internet, i.e. size can only be determined later
- Aspect ratio for each image will vary; some are tall, some are square, some are flat
- ImageView's heightneeds to change, either bigger or smaller based on the aspect ratio of the
- Excess height is cropped, can't have a lot of empty space like it is by default.
- 图像为固定宽度(屏幕宽度的大小)
- 图片是网上下载的,即大小只能后期确定
- 每个图像的纵横比会有所不同;有的高,有的方形,有的扁平
- ImageView 的高度需要改变,根据图像的纵横比变大或变小
- 多余的高度被裁剪,不能像默认情况下有很多空白空间。
For example, a tiny 50x50 image on a 400 px width screen would scale the image up to 400x400 px. A 800x200 image would scale to 400x100.
例如,400 像素宽度的屏幕上的 50x50 小图像会将图像缩放到 400x400 像素。800x200 的图像将缩放到 400x100。
I've looked at most of the other threads, and many proposed solutions like simply changing adjustViewBounds
and scaleType
will only scale down an image, and not scale it up.
我已经查看了大多数其他线程,许多提议的解决方案就像简单地改变adjustViewBounds
并且scaleType
只会缩小图像,而不是放大图像。
采纳答案by Muz
ImageView mImageView; // This is the ImageView to change
// Use this in onWindowFocusChanged so that the ImageView is fully loaded, or the dimensions will end up 0.
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
// Abstracting out the process where you get the image from the internet
Bitmap loadedImage = getImageFromInternet (url);
// Gets the width you want it to be
intendedWidth = mImageView.getWidth();
// Gets the downloaded image dimensions
int originalWidth = loadedImage.getWidth();
int originalHeight = loadedImage.getHeight();
// Calculates the new dimensions
float scale = (float) intendedWidth / originalWidth;
int newHeight = (int) Math.round(originalHeight * scale);
// Resizes mImageView. Change "FrameLayout" to whatever layout mImageView is located in.
mImageView.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT));
mImageView.getLayoutParams().width = intendedWidth;
mImageView.getLayoutParams().height = newHeight;
}
回答by Akshat Agarwal
I created it with the help of this Bob Lee's answer in this post: Android: How to stretch an image to the screen width while maintaining aspect ratio?
我在 Bob Lee 在这篇文章中的回答的帮助下创建了它:Android:如何在保持纵横比的同时将图像拉伸到屏幕宽度?
package com.yourpackage.widgets;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AspectRatioImageView extends ImageView {
public AspectRatioImageView(Context context) {
super(context);
}
public AspectRatioImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
setMeasuredDimension(width, height);
}
}
Now to use it in the XML:
现在在 XML 中使用它:
<com.yourpackage.widgets.AspectRatioImageView android:layout_centerHorizontal="true"
android:src="@drawable/yourdrawable" android:id="@+id/image"
android:layout_alignParentTop="true" android:layout_height="wrap_content"
android:layout_width="match_parent" android:adjustViewBounds="true" />
Have fun!
玩得开心!
回答by hamid toosi
I prefer Agarwal's answer with some modifications, because it is resuable:
我更喜欢 Agarwal 的答案,并进行了一些修改,因为它是可重用的:
package com.yourpackage.widgets;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.ViewGroup;
public class AspectRatioImageView extends AppCompatImageView {
public AspectRatioImageView(Context context)
{
super(context);
this.setScaleType(ScaleType.FIT_XY);
}
public AspectRatioImageView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setScaleType(ScaleType.FIT_XY);
}
public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setScaleType(ScaleType.FIT_XY);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
Drawable d = getDrawable();
if (d != null && d.getIntrinsicHeight() > 0)
{
int width = getLayoutParams().width;
int height = getLayoutParams().height;
// ?? ??? ???? ????? ??????? ?? ?? ???? ??? ?? ??? ?? ????
if (height == ViewGroup.LayoutParams.WRAP_CONTENT || (width > 0 && width < 10000))
{
if (width <= 0);
width = MeasureSpec.getSize(widthMeasureSpec);
if (width > 0)
height = width * d.getIntrinsicHeight() / d.getIntrinsicWidth();
// ?????? ????? ?? ?????? ?????? ????? ????
if (height > getMaxHeight()) {
height = getMaxHeight();
width = height * d.getIntrinsicWidth() / d.getIntrinsicHeight();
}
}
else if (width == ViewGroup.LayoutParams.WRAP_CONTENT || (height > 0 && height < 10000))
{
// ??? ?????? ???? ?????? ???? ???? ??? ?????? ??? ?? ?? ??? ?? ?????
if (height <= 0)
height = MeasureSpec.getSize(heightMeasureSpec);
// ??? ?????? ???? ??? ?? ?? ????? ??? ?? ?? ????? ???? ????
if (height > 0)
width = height * d.getIntrinsicWidth() / d.getIntrinsicHeight();
// ??? ????? ?? ?????? ??? ????? ????
if (width > getMaxWidth()) {
width = getMaxWidth();
height = width * d.getIntrinsicHeight() / d.getIntrinsicWidth();
}
}
// ??? ??????? ?????? ???? ???
if (width > 0 && height > 0)
setMeasuredDimension(width, height);
// ?? ??? ??????? ??? ?? ?? ?? ????? ?? ??? ????? ???
else
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
else
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Now to use it in the XML:
现在在 XML 中使用它:
<com.yourpackage.widgets.AspectRatioImageView
android:src="@drawable/yourdrawable"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>