2012-09-13 51 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"/> 
相關問題