Resizing ImageView to fit to aspect ratio

后端 未结 3 1024
夕颜
夕颜 2020-11-30 06:20

I need to resize an ImageView such that it fits to the screen, maintains the same aspect ratio. The following conditions hold:

  • Images are a fixed width (size o
3条回答
  •  清歌不尽
    2020-11-30 06:45

    I prefer Agarwal's answer with some modifications, because it is resuable:

    package com.yourpackage.widgets;
    
    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.support.v7.widget.AppCompatImageView;
    import android.util.AttributeSet;
    import android.view.ViewGroup;
    
    public class AspectRatioImageView extends AppCompatImageView {
    
        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.getIntrinsicHeight() > 0)
            {
                int width = getLayoutParams().width;
                int height = getLayoutParams().height;
    
                // تو این حالت مبنای محاسبات رو بر اساس طول در نظر می گیرم
                if (height == ViewGroup.LayoutParams.WRAP_CONTENT || (width > 0 &&  width < 10000))
                {
                    if (width <= 0);
                    width = MeasureSpec.getSize(widthMeasureSpec);
    
                    if (width > 0)
                        height = width * d.getIntrinsicHeight() / d.getIntrinsicWidth();
    
                    // ارتفاع نباید از بیشینه ارتفاع بیشتر باشه
                    if (height > getMaxHeight()) {
                        height = getMaxHeight();
                        width = height * d.getIntrinsicWidth() / d.getIntrinsicHeight();
                    }
    
                }
                else if (width == ViewGroup.LayoutParams.WRAP_CONTENT  || (height > 0 &&  height < 10000))
                {
                    // اگر مقداری برای ارتفاع مشخص نشده بود ارتفاع پدر رو در نظر می گیریم
                    if (height <= 0)
                        height = MeasureSpec.getSize(heightMeasureSpec);
    
                    // اگه ارتفاع مشخص بود که می تونیم طول رو بر اساسش حساب کنیم
                    if (height > 0)
                        width = height * d.getIntrinsicWidth() / d.getIntrinsicHeight();
    
                    // طول نباید از بیشینه طول بیشتر باشه
                    if (width > getMaxWidth()) {
                        width = getMaxWidth();
                        height = width * d.getIntrinsicHeight() / d.getIntrinsicWidth();
                    }
                }
    
                // اگه محاسبات موفقیت آمیز بود
                if (width > 0 && height > 0)
                    setMeasuredDimension(width, height);
                    // در غیر اینصورت همه چی رو می سپریم به خود ایمیج ویو
                else
                    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
            else
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    

    Now to use it in the XML:

    
    

提交回复
热议问题