/* * Copyright (c) 2015 Zhang Hai <Dreaming.in.Code.ZH@Gmail.com> * All Rights Reserved. */ package me.zhanghai.android.douya.ui; import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.util.AttributeSet; import android.widget.FrameLayout; public class RatioFrameLayout extends FrameLayout { private float mRatio; public RatioFrameLayout(Context context) { super(context); } public RatioFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); } public RatioFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public RatioFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public float getRatio() { return mRatio; } public void setRatio(float ratio) { if (mRatio != ratio) { mRatio = ratio; requestLayout(); invalidate(); } } public void setRatio(float width, float height) { setRatio(width / height); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mRatio > 0) { if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) { int height = MeasureSpec.getSize(heightMeasureSpec); int width = Math.round(mRatio * height); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { width = Math.max(width, getMinimumWidth()); } widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); } else { int width = MeasureSpec.getSize(widthMeasureSpec); int height = Math.round(width / mRatio); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { height = Math.max(height, getMinimumHeight()); } heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }