2012-09-13 52 views
12

我需要调整ImageView的大小,使其适合屏幕,保持相同的宽高比。下列条件成立:调整ImageView的大小以适合宽高比

  • 图像是一个固定的宽度(屏幕的宽度的尺寸)
  • 图像被从因特网下载的,即尺寸只能在以后确定
  • 长宽比为每个图像将变化;一些是高大的,有些是正方形的,有些是平坦
  • 的ImageView 高度需要改变,或者更大或基于所述
  • 过量高度被裁剪的纵横比变小,不能有大量的空的空间就像它是默认的那样。

例如,400 px宽度屏幕上的小型50x50图像会将图像缩放到400x400像素。 800x200的图像可以缩放到400x100。

我已经看过了大多数其他线程,许多建议的解决方案(如简单更改adjustViewBoundsscaleType)只会缩放图像,而不会扩展图像。

回答

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

我这个鲍勃·李的回答在这个岗位帮助创建它:Android: How to stretch an image to the screen width while maintaining aspect ratio?

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

我们用它在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" /> 

玩得开心!

+4

+1,美观大方,操作简单,只有我会保护它对'getIntrinsicWidth'为0(它会崩溃尝试除以它) –

+0

adjustViewBounds也应该在AspectRatioImageView中完成http://developer.android.com/reference/android/widget/ImageView.html#setAdjustViewBounds%28boolean%29 – Nepster

1

我喜欢阿加瓦尔的一些修改答案,因为它是可重复使用:

package com.yourpackage.widgets; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.widget.ImageView; 

public class AspectRatioImageView extends ImageView { 

    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.getIntrinsicWidth() > 0) 
     { 
      int width = MeasureSpec.getSize(widthMeasureSpec); 
      if (width <= 0) 
       width = getLayoutParams().width; 

      int height = width * d.getIntrinsicHeight()/d.getIntrinsicWidth(); 
      setMeasuredDimension(width, height); 
     } 
     else 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
    } 
} 

现在在XML使用它:

<com.yourpackage.widgets.AspectRatioImageView 
android:src="@drawable/yourdrawable" 
android:layout_height="wrap_content" 
android:layout_width="match_parent"/> 
相关问题