Fixed aspect ratio View

后端 未结 9 1800
后悔当初
后悔当初 2020-12-04 10:50

How would I go implementing a fixed aspect ratio View? I\'d like to have items with 1:1 aspect ratio in a GridView. I think it\'s better to subclas

9条回答
  •  失恋的感觉
    2020-12-04 11:35

    I implemented FixedAspectRatioFrameLayout, so I can reuse it and have any hosted view be with fixed aspect ratio:

    public class FixedAspectRatioFrameLayout extends FrameLayout
    {
        private int mAspectRatioWidth;
        private int mAspectRatioHeight;
    
        public FixedAspectRatioFrameLayout(Context context)
        {
            super(context);
        }
    
        public FixedAspectRatioFrameLayout(Context context, AttributeSet attrs)
        {
            super(context, attrs);
    
            init(context, attrs);
        }
    
        public FixedAspectRatioFrameLayout(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
    
            init(context, attrs);
        }
    
        private void init(Context context, AttributeSet attrs)
        {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FixedAspectRatioFrameLayout);
    
            mAspectRatioWidth = a.getInt(R.styleable.FixedAspectRatioFrameLayout_aspectRatioWidth, 4);
            mAspectRatioHeight = a.getInt(R.styleable.FixedAspectRatioFrameLayout_aspectRatioHeight, 3);
    
            a.recycle();
        }
        // **overrides**
    
        @Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
        {
            int originalWidth = MeasureSpec.getSize(widthMeasureSpec);
    
            int originalHeight = MeasureSpec.getSize(heightMeasureSpec);
    
            int calculatedHeight = originalWidth * mAspectRatioHeight / mAspectRatioWidth;
    
            int finalWidth, finalHeight;
    
            if (calculatedHeight > originalHeight)
            {
                finalWidth = originalHeight * mAspectRatioWidth / mAspectRatioHeight; 
                finalHeight = originalHeight;
            }
            else
            {
                finalWidth = originalWidth;
                finalHeight = calculatedHeight;
            }
    
            super.onMeasure(
                    MeasureSpec.makeMeasureSpec(finalWidth, MeasureSpec.EXACTLY), 
                    MeasureSpec.makeMeasureSpec(finalHeight, MeasureSpec.EXACTLY));
        }
    }
    

提交回复
热议问题