2012-09-17 15 views
13

कोड के अंदर एक छवि का दिखाया गया है आकार जाओ सरल है:एक imageView

<ImageView android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@drawable/cat"/> 

सूचना imageView चौड़ाई और ऊंचाई के लिए fill_parent इस्तेमाल किया।

छवि cat एक छोटी छवि है और इसे छवि दृश्य में फिट करने के लिए ज़ूम किया जाएगा, और एक ही समय में चौड़ाई/ऊंचाई अनुपात बनाए रखें।

मेरा प्रश्न है कि छवि के प्रदर्शित आकार को कैसे प्राप्त करें? मैंने कोशिश की:

imageView.getDrawable().getIntrinsicHeight() 

लेकिन जो इसे छवि cat के मूल ऊंचाई।

मैंने कोशिश की:

imageView.getDrawable().getBounds() 

लेकिन जो Rect(0,0,0,0) देता है।

+0

आप ViewTreeObserver का उपयोग कर सकते रन टाइम http://developer.android.com/reference/android/view/ViewTreeObserver.html –

+0

धन्यवाद पर किसी भी दृश्य की वास्तविक आयाम प्राप्त करने के लिए, मैं इसे देख रहा हूँ। पीएस: क्या यह आकार पाने का सबसे अच्छा/एकमात्र तरीका है? – Freewind

+1

क्षमा करें, यहां तक ​​कि ViewTreeObserver के साथ, मुझे अभी भी पता नहीं है कि छवि के ** प्रदर्शित ** आकार को कैसे प्राप्त करें। – Freewind

उत्तर

39

निम्नलिखित काम करेगा:

ih=imageView.getMeasuredHeight();//height of imageView 
iw=imageView.getMeasuredWidth();//width of imageView 
iH=imageView.getDrawable().getIntrinsicHeight();//original height of underlying image 
iW=imageView.getDrawable().getIntrinsicWidth();//original width of underlying image 

if (ih/iH<=iw/iW) iw=iW*ih/iH;//rescaled width of image within ImageView 
else ih= iH*iw/iW;//rescaled height of image within ImageView 

(iw एक्स IH) अब वास्तविक पुनः पैमाना भीतर छवि के लिए (चौड़ाई x ऊँचाई) (का प्रतिनिधित्व करता है, दूसरे शब्दों में छवि का प्रदर्शित आकार)


संपादित करें: मुझे लगता है कि उपर्युक्त उत्तर लिखने का एक अच्छा तरीका है (और जो काम करता है टीएस):

  final int actualHeight, actualWidth; 
      final int imageViewHeight = imageView.getHeight(), imageViewWidth = imageView.getWidth(); 
      final int bitmapHeight = ..., bitmapWidth = ...; 
      if (imageViewHeight * bitmapWidth <= imageViewWidth * bitmapHeight) { 
       actualWidth = bitmapWidth * imageViewHeight/bitmapHeight; 
       actualHeight = imageViewHeight; 
      } else { 
       actualHeight = bitmapHeight * imageViewWidth/bitmapWidth; 
       actualWidth = imageViewWidth; 
      } 

      return new Point(actualWidth,actualHeight); 
+0

int int परिणाम int int के साथ int को विभाजित करने के बारे में मत भूलना। :) – abbath

+8

जबकि मुझे यह जवाब पसंद है, मुझे कहना होगा कि मुझे आपकी परिवर्तनीय नामकरण योजना पसंद नहीं है :( –

+0

@MattLogan मैंने इसे एक अच्छा लेखन करने के लिए संपादित किया है। आशा है कि आप इसे बेहतर पसंद करेंगे। –

-2

उपयोग

// For getting imageview height 
imgObj.getMeasuredHeight() 


// For getting imageview width 
imgObj.getMeasuredWidth(); 


//For getting image height inside ImageView 
imgObj.getDrawable().getIntrinsicHeight(); 


//For getting image width inside ImageView 
imgObj.getDrawable().getIntrinsicWidth(); 
1

छवि दृश्य में छवि की सीमा प्राप्त करने के लिए यहां एक सहायक कार्य है।

/** 
* Helper method to get the bounds of image inside the imageView. 
* 
* @param imageView the imageView. 
* @return bounding rectangle of the image. 
*/ 
public static RectF getImageBounds(ImageView imageView) { 
    RectF bounds = new RectF(); 
    Drawable drawable = imageView.getDrawable(); 
    if (drawable != null) { 
     imageView.getImageMatrix().mapRect(bounds, new RectF(drawable.getBounds())); 
    } 
    return bounds; 
} 
संबंधित मुद्दे