2012-09-13 10 views
12

Tôi cần đổi kích thước ImageView sao cho phù hợp với màn hình, duy trì cùng tỷ lệ khung hình. Các điều kiện sau giữ:Thay đổi kích thước ImageView để vừa với tỷ lệ khung hình

  • hình ảnh là một chiều rộng cố định (kích thước chiều rộng của màn hình)
  • hình ảnh được tải xuống từ Internet, tức là kích thước chỉ có thể được xác định sau
  • tỷ lệ
  • Aspect cho mỗi hình ảnh sẽ thay đổi; một số là cao, một số là hình vuông, một số là phẳng
  • ImageView của chiều cao cần phải thay đổi, hoặc là lớn hơn hoặc nhỏ hơn dựa trên tỷ lệ khía cạnh của
  • chiều cao dư thừa được cắt, không thể có rất nhiều không gian trống như nó là theo mặc định.

Ví dụ: hình ảnh 50x50 nhỏ trên màn hình chiều rộng 400 px sẽ chia tỷ lệ hình ảnh lên đến 400x400 pixel. Hình ảnh 800x200 sẽ có kích thước 400x100.

Tôi đã xem hầu hết các chủ đề khác và nhiều giải pháp được đề xuất như chỉ cần thay đổi adjustViewBoundsscaleType sẽ chỉ giảm kích thước hình ảnh và không mở rộng quy mô.

Trả lời

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

tôi tạo ra nó với sự giúp đỡ của câu trả lời này Bob Lee trong bài viết này: 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); 
    } 
} 

Bây giờ sử dụng nó trong 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" /> 

Hãy vui vẻ!

+4

+1, đẹp và đơn giản, duy nhất tôi sẽ bảo vệ nó chống lại 'getIntrinsicWidth' là 0 (nó sẽ sụp đổ cố gắng để phân chia bởi nó) –

+0

adjustViewBounds cũng nên được thực hiện trong AspectRatioImageView http://developer.android.com/reference/android/widget/ImageView.html#setAdjustViewBounds%28boolean%29 – Nepster

1

Tôi thích câu trả lời Agarwal với một số sửa đổi, bởi vì nó là resuable:

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

Bây giờ sử dụng nó trong XML:

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