2012-03-28 12 views
11

मैं दोनों मिनटों को निर्दिष्ट करना चाहता हूं। चौड़ाई/ऊंचाई और अधिकतम। एक छवि दृश्य के लिए चौड़ाई/ऊंचाई। छवि फिट करने के लिए स्केल किया जाना चाहिए।Android में एक छवि दृश्य के लिए न्यूनतम और अधिकतम आकार दोनों को कैसे सेट करें

ऐसा करने से:

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:minWidth="100dp" 
    android:maxWidth="200dp" 
    android:minHeight="100dp" 
    android:maxHeight="200dp" 
    android:src="@drawable/ic_launcher" /> 

मि। चौड़ाई और ऊंचाई प्रदर्शन करते हैं, लेकिन अधिकतम। चौड़ाई और ऊंचाई को नजरअंदाज कर दिया जाता है।

अगर मैं एंड्रॉयड सेट: adjustViewBounds = "true":

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:adjustViewBounds="true" 
    android:scaleType="fitCenter" 
    android:minWidth="100dp" 
    android:maxWidth="200dp" 
    android:minHeight="100dp" 
    android:maxHeight="200dp" 
    android:src="@drawable/ic_launcher" /> 
से

, अधिकतम। चौड़ाई और ऊंचाई प्रदर्शन करते हैं, लेकिन न्यूनतम। चौड़ाई और ऊंचाई को नजरअंदाज कर दिया जाता है।

क्या दोनों के पास कोई तरीका है?

public class ResizingImageView extends ImageView { 

    private int mMaxWidth; 
    private int mMaxHeight; 

    public ResizingImageView(Context context) { 
     super(context); 
    } 

    public ResizingImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ResizingImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void setMaxWidth(int maxWidth) { 
     super.setMaxWidth(maxWidth); 
     mMaxWidth = maxWidth; 
    } 

    @Override 
    public void setMaxHeight(int maxHeight) { 
     super.setMaxHeight(maxHeight); 
     mMaxHeight = maxHeight; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     Drawable drawable = getDrawable(); 
     if (drawable != null) { 

      int wMode = MeasureSpec.getMode(widthMeasureSpec); 
      int hMode = MeasureSpec.getMode(heightMeasureSpec); 
      if (wMode == MeasureSpec.EXACTLY || hMode == MeasureSpec.EXACTLY) { 
       return; 
      } 

      // Calculate the most appropriate size for the view. Take into 
      // account minWidth, minHeight, maxWith, maxHeigh and allowed size 
      // for the view. 

      int maxWidth = wMode == MeasureSpec.AT_MOST 
        ? Math.min(MeasureSpec.getSize(widthMeasureSpec), mMaxWidth) 
        : mMaxWidth; 
      int maxHeight = hMode == MeasureSpec.AT_MOST 
        ? Math.min(MeasureSpec.getSize(heightMeasureSpec), mMaxHeight) 
        : mMaxHeight; 

      int dWidth = Helpers.dipsToPixels(drawable.getIntrinsicWidth()); 
      int dHeight = Helpers.dipsToPixels(drawable.getIntrinsicHeight()); 
      float ratio = ((float) dWidth)/dHeight; 

      int width = Math.min(Math.max(dWidth, getSuggestedMinimumWidth()), maxWidth); 
      int height = (int) (width/ratio); 

      height = Math.min(Math.max(height, getSuggestedMinimumHeight()), maxHeight); 
      width = (int) (height * ratio); 

      if (width > maxWidth) { 
       width = maxWidth; 
       height = (int) (width/ratio); 
      } 

      setMeasuredDimension(width, height); 
     } 
    } 
} 

यह दृश्य अब की तरह इस्तेमाल किया जा सकता:

उत्तर

11

अंत में, मैं अपने खुद के विचार है कि imageView extands और अधिलेखित कर देता है onMeasure() बनाया गया

<my.package.ResizingImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:minWidth="100dp" 
    android:maxWidth="200dp" 
    android:minHeight="100dp" 
    android:maxHeight="200dp" 
    android:src="@drawable/ic_launcher" /> 

यह काम करता है के रूप में मैं चाहता था, लेकिन क्या कोई आसान समाधान नहीं होना चाहिए?

+12

आप सही हैं। मैं एंड्रॉइड के साथ सरल काम करने में सक्षम नहीं होने के थक गया हूँ। – tasomaniac

0
 <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:minWidth="150dp" 
      android:minHeight="150dp" 
      > 

      <RelativeLayout 
       android:id="@+id/imageLayout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@color/white" 
       android:layout_gravity="center_vertical" 
       > 

       <ImageView 
        android:id="@+id/image" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:adjustViewBounds="true" 
        android:src="@drawable/trump" 

        /> 

       <ProgressBar 
        android:id="@+id/progressBar" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true" 
        android:visibility="gone" /> 
      </RelativeLayout> 
     </LinearLayout> 

कंटेनर लेआउट में न्यूनतम ऊंचाई और चौड़ाई दें और छविदृश्य में एडव्यू व्यूबाउंड दें। इस तरह आपका काम सरल चरणों में पूरा हो जाएगा।

संबंधित मुद्दे