调整大小的ImageView以适合长宽比 [英] Resizing ImageView to fit to aspect ratio

查看:354
本文介绍了调整大小的ImageView以适合长宽比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调整的ImageView的,使得它适合屏幕,保持同样的纵横比。下列条件成立:

  • 图片是一个固定的宽度(屏幕宽度的尺寸)
  • 图片都是从网上下载的,即大小只能在以后确定
  • 宽高比为每个图像会有所不同;有些高,有些是方形的,有些是平坦
  • 的ImageView的的需要改变,或者根据
  • 的长宽比大或变小
  • 在过量的高度被裁剪,不能有大量的空白空间就像是在默认情况下。

例如,一个400像素宽的屏幕上一个小小的50×50的图像将缩放图像高达400×400像素。一个800x200的图像会扩展到400x100。

我看了大部分的其他线程,而像简单地改变许多建议的解决方案 adjustViewBounds scaleType 只有缩小图像,而不是将其放大。

解决方案

  ImageView的mImageView; //这是ImageView的改变

//使用这onWindowFocusChanged使得ImageView的满载,或尺寸将最终0。
公共无效onWindowFocusChanged(布尔hasFocus)
{
    super.onWindowFocusChanged(hasFocus);

    //抽象出来,你从互联网上获取图像的过程
    位图loadedImage = getImageFromInternet(URL);

    //获取您希望它是宽度
    intendedWidth = mImageView.getWidth();

    //获取下载的图片尺寸
    INT originalWidth = loadedImage.getWidth();
    INT originalHeight = loadedImage.getHeight();

    //计算新维度
    浮规模=(浮点)intendedWidth / originalWidth;
    INT newHeight =(INT)Math.round(originalHeight *等级);

    //调整大小mImageView。变的FrameLayout的任何布局mImageView位于研究。
    mImageView.setLayoutParams(新FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT,
            FrameLayout.LayoutParams.WRAP_CONTENT));
    。mImageView.getLayoutParams()宽度= intendedWidth;
    。mImageView.getLayoutParams()高度= newHeight;
}
 

I need to resize an ImageView such that it fits to the screen, maintains the same aspect ratio. The following conditions hold:

  • 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 height needs 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.

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.

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.

解决方案

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;
}

这篇关于调整大小的ImageView以适合长宽比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆