2012-09-13 16 views
12

पर फिट करने के लिए ImageView का आकार बदलना मुझे एक छवि दृश्य का आकार बदलने की आवश्यकता है जैसे कि यह स्क्रीन पर फिट बैठता है, वही पहलू अनुपात बनाए रखता है। निम्न स्थितियों में पकड़:पहलू अनुपात

  • छवियाँ एक निश्चित चौड़ाई (स्क्रीन की चौड़ाई का आकार)
  • छवियाँ इंटरनेट से डाउनलोड कर रहे हैं, यानी आकार केवल बाद में निर्धारित किया जा सकता
  • प्रत्येक छवि के लिए
  • पहलू अनुपात होगा भिन्न है, कुछ लंबे हैं, कुछ, वर्ग हैं कुछ सपाट हैं
  • imageView के ऊंचाई बदलने की जरूरत है, या तो बड़े या
  • अतिरिक्त ऊंचाई काटी है पहलू अनुपात के आधार पर छोटे, खाली जगह का एक बहुत नहीं हो सकता जैसे यह डिफ़ॉल्ट रूप से है।

उदाहरण के लिए, 400 पीएक्स चौड़ाई स्क्रीन पर एक छोटी 50x50 छवि 400x400 पीएक्स तक छवि को स्केल करेगी। 800x200 छवि 400x100 तक स्केल होगी।

मैंने अन्य थ्रेडों को देखा है, और कई प्रस्तावित समाधान जैसे कि adjustViewBounds और scaleType बदलना केवल एक छवि को स्केल करेगा, और इसे स्केल नहीं करेगा।

उत्तर

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

एडजस्ट व्यूबाउंड्स AspectRatioImageView http://developer.android.com/reference/android/widget/ImageView.html#setAdjustViewBounds%28boolean%29 में भी किया जाना चाहिए – Nepster

1

मैं, कुछ संशोधनों के साथ अग्रवाल का जवाब पसंद करते हैं क्योंकि यह 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);  
    } 
} 

अब XML में ठीक से उपयोग करने के लिए:

<com.yourpackage.widgets.AspectRatioImageView 
android:src="@drawable/yourdrawable" 
android:layout_height="wrap_content" 
android:layout_width="match_parent"/> 
संबंधित मुद्दे