diff options
| -rw-r--r-- | res/values/attrs.xml | 24 | ||||
| -rw-r--r-- | src/com/android/email/view/SizeBoundingFrameLayout.java | 115 |
2 files changed, 139 insertions, 0 deletions
diff --git a/res/values/attrs.xml b/res/values/attrs.xml new file mode 100644 index 000000000..8521cf48a --- /dev/null +++ b/res/values/attrs.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2011 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<!-- Defines attributes for custom views --> + +<resources> + <declare-styleable name="SizeBoundingFrameLayout_attributes"> + <attr name="maxWidth" format="dimension"/> + <attr name="maxHeight" format="dimension"/> + </declare-styleable> +</resources> diff --git a/src/com/android/email/view/SizeBoundingFrameLayout.java b/src/com/android/email/view/SizeBoundingFrameLayout.java new file mode 100644 index 000000000..b1e709a0c --- /dev/null +++ b/src/com/android/email/view/SizeBoundingFrameLayout.java @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.email.view; + +import com.android.email.R; + +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import android.widget.FrameLayout; + +/** + * A {@link FrameLayout} with the max width/height. + */ +public class SizeBoundingFrameLayout extends FrameLayout { + public static final int DIMENSION_DEFAULT = -1; // unspecified + + private int mMaxWidth = DIMENSION_DEFAULT; + private int mMaxHeight = DIMENSION_DEFAULT; + + public SizeBoundingFrameLayout(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + initFromAttributeSet(context, attrs); + } + + public SizeBoundingFrameLayout(Context context, AttributeSet attrs) { + super(context, attrs); + initFromAttributeSet(context, attrs); + } + + public SizeBoundingFrameLayout(Context context) { + super(context); + } + + private void initFromAttributeSet(Context c, AttributeSet attrs) { + TypedArray a = c.obtainStyledAttributes(attrs, + R.styleable.SizeBoundingFrameLayout_attributes); + mMaxWidth = a.getDimensionPixelSize( + R.styleable.SizeBoundingFrameLayout_attributes_maxWidth, DIMENSION_DEFAULT); + mMaxHeight = a.getDimensionPixelSize( + R.styleable.SizeBoundingFrameLayout_attributes_maxHeight, DIMENSION_DEFAULT); + a.recycle(); + } + + /** Set the max width. Use {@link #DIMENSION_DEFAULT} for unspecified. */ + public void setMaxWidth(int maxWidth) { + mMaxWidth = maxWidth; + requestLayout(); + invalidate(); + } + + public int getMaxWidth() { + return mMaxWidth; + } + + /** Set the max height. Use {@link #DIMENSION_DEFAULT} for unspecified. */ + public void setMaxHeight(int maxHeight) { + mMaxHeight = maxHeight; + requestLayout(); + invalidate(); + } + + public int getMaxHeight() { + return mMaxHeight; + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + int widthMode = MeasureSpec.getMode(widthMeasureSpec); + int heightMode = MeasureSpec.getMode(heightMeasureSpec); + int widthSize = MeasureSpec.getSize(widthMeasureSpec); + int heightSize = MeasureSpec.getSize(heightMeasureSpec); + + // Limit the size, unless MeasureSpec.EXACTLY + if (mMaxWidth >= 0) { + switch (widthMode) { + case MeasureSpec.AT_MOST: + widthSize = Math.min(widthSize, mMaxWidth); + break; + case MeasureSpec.UNSPECIFIED: + widthMode = MeasureSpec.AT_MOST; + widthSize = mMaxWidth; + break; + } + } + + if (mMaxHeight >= 0) { + switch (heightMode) { + case MeasureSpec.AT_MOST: + heightSize = Math.min(heightSize, mMaxHeight); + break; + case MeasureSpec.UNSPECIFIED: + heightMode = MeasureSpec.AT_MOST; + heightSize = mMaxHeight; + break; + } + } + super.onMeasure(MeasureSpec.makeMeasureSpec(widthSize, widthMode), + MeasureSpec.makeMeasureSpec(heightSize, heightMode)); + } +} |
