package com.circlegate.liban.view; import com.circlegate.liban.R; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.widget.FrameLayout; public class BoundedFrameLayout extends FrameLayout { private int mMaxWidth; private int mMaxHeight; public BoundedFrameLayout(Context context) { this(context, null); } public BoundedFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); if (attrs != null) { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.BoundedView); mMaxWidth = a.getDimensionPixelSize(R.styleable.BoundedView_android_maxWidth, 0); mMaxHeight = a.getDimensionPixelSize(R.styleable.BoundedView_android_maxHeight, 0); a.recycle(); } else { mMaxWidth = 0; mMaxHeight = 0; } } public int getMaximumWidth() { return this.mMaxWidth; } public int getMaximumHeight() { return this.mMaxHeight; } public void setMaximumWidth(int maxWidth) { if (this.mMaxWidth != maxWidth) { this.mMaxWidth = maxWidth; requestLayout(); } } public void setMaximumHeigth(int maxHeight) { if (this.mMaxHeight != maxHeight) { this.mMaxHeight = maxHeight; requestLayout(); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // Adjust width as necessary int measuredWidth = MeasureSpec.getSize(widthMeasureSpec); if (mMaxWidth > 0 && mMaxWidth < measuredWidth) { int measureMode = MeasureSpec.getMode(widthMeasureSpec); widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, measureMode); } // Adjust height as necessary int measuredHeight = MeasureSpec.getSize(heightMeasureSpec); if (mMaxHeight > 0 && mMaxHeight < measuredHeight) { int measureMode = MeasureSpec.getMode(heightMeasureSpec); heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, measureMode); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }