如何在Android中为ImageView设置最小和最大尺寸

问题描述:

我想这样指定最小。宽度/高度和最大值ImageView的宽度/高度。图像应该缩放以适合。如何在Android中为ImageView设置最小和最大尺寸

这样做:

<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" /> 

最小。宽度和高度按照它们应该执行,但最大。宽度和高度被忽略。

如果我设置的android:adjustViewBounds = “真”:

<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" /> 

比,最大。宽度和高度按照它们应该执行,但最小。宽度和高度被忽略。

有没有办法兼得?

最后,我创建了自己的观点,即extands ImageView的,并覆盖onMeasure():

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

这种观点,现在喜欢使用:

<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

你说得对。我厌倦了无法用Android做简单的事情。 – tasomaniac 2012-12-18 20:35:59

 <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> 

请参阅给容器布局分高度和宽度,并在Imageview中放置adjustViewBounds。这样你的任务将以简单的步骤完成。