Chromium 里自带的可以限制最大宽高的自定义 View。 自己本来写了一个,思路差不多。但是一读它的类,果然还是官方写的专业严谨。这里记录一下
Last active
February 28, 2018 02:54
-
-
Save s1ntoneli/d7ae40a28c300051012becb9350802a2 to your computer and use it in GitHub Desktop.
限制最大宽高的自定义 View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <declare-styleable name="BoundedView"> | |
| <attr name="maxWidth" format="dimension" /> | |
| <attr name="maxHeight" format="dimension" /> | |
| </declare-styleable> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * A LinearLayout that can be constrained to a maximum size. | |
| * | |
| * Example: | |
| * <org.chromium.chrome.browser.widget.BoundedLinearLayout | |
| * xmlns:android="http://schemas.android.com/apk/res/android" | |
| * xmlns:chrome="http://schemas.android.com/apk/res-auto" | |
| * android:layout_width="match_parent" | |
| * android:layout_height="match_parent" | |
| * chrome:maxWidth="692dp" > | |
| * ... | |
| */ | |
| public class BoundedLinearLayout extends LinearLayout { | |
| private static final int NOT_SPECIFIED = -1; | |
| private final int mMaxWidth; | |
| private final int mMaxHeight; | |
| /** | |
| * Constructor for inflating from XML. | |
| */ | |
| public BoundedLinearLayout(Context context, AttributeSet attrs) { | |
| super(context, attrs); | |
| TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BoundedView); | |
| int maxWidth = a.getDimensionPixelSize(R.styleable.BoundedView_maxWidth, NOT_SPECIFIED); | |
| int maxHeight = a.getDimensionPixelSize(R.styleable.BoundedView_maxHeight, NOT_SPECIFIED); | |
| a.recycle(); | |
| // Treat 0 or below as being unconstrained. | |
| mMaxWidth = maxWidth <= 0 ? NOT_SPECIFIED : maxWidth; | |
| mMaxHeight = maxHeight <= 0 ? NOT_SPECIFIED : maxHeight; | |
| } | |
| @Override | |
| protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
| // Limit width to mMaxWidth. | |
| int widthSize = MeasureSpec.getSize(widthMeasureSpec); | |
| if (mMaxWidth != NOT_SPECIFIED && widthSize > mMaxWidth) { | |
| int widthMode = MeasureSpec.getMode(widthMeasureSpec); | |
| if (widthMode == MeasureSpec.UNSPECIFIED) widthMode = MeasureSpec.AT_MOST; | |
| widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, widthMode); | |
| } | |
| int heightSize = MeasureSpec.getSize(heightMeasureSpec); | |
| if (mMaxHeight != NOT_SPECIFIED && heightSize > mMaxHeight) { | |
| int heightMode = MeasureSpec.getMode(heightMeasureSpec); | |
| if (heightMode == MeasureSpec.UNSPECIFIED) heightMode = MeasureSpec.AT_MOST; | |
| heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, heightMode); | |
| } | |
| super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment