2012-03-28 52 views
11

Tôi muốn chỉ định cả hai phút. chiều rộng/chiều cao và tối đa. chiều rộng/chiều cao cho một ImageView. Hình ảnh phải được thu nhỏ để vừa.Cách đặt cả kích thước tối thiểu và tối đa cho một ImageView trong Android

Việc làm này:

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

min. chiều rộng và chiều cao thực hiện như chúng cần, nhưng tối đa. chiều rộng và chiều cao được bỏ qua.

Nếu tôi đặt android: 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" /> 

hơn, tối đa. chiều rộng và chiều cao thực hiện như họ cần, nhưng min. chiều rộng và chiều cao được bỏ qua.

Có cách nào để có cả hai?

Trả lời

11

Cuối cùng, tôi đã tạo cái nhìn của riêng tôi mà extands ImageView và ghi đè 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); 
     } 
    } 
} 

Quan điểm này bây giờ có thể được sử dụng như:

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

này hoạt động như tôi muốn, nhưng không nên có một giải pháp dễ dàng hơn?

+12

Bạn nói đúng. Tôi mệt mỏi vì không thể làm điều đơn giản với Android. – 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> 

Xem Đặt chiều cao và chiều rộng tối thiểu cho bố cục vùng chứa và đặt adjustmentViewBounds trong Imageview. Bằng cách này, nhiệm vụ của bạn sẽ được hoàn thành trong các bước đơn giản.

Các vấn đề liên quan