Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.contacts.widget; |
| 18 | |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 21 | import android.graphics.Rect; |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 22 | import android.util.AttributeSet; |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 23 | import android.view.Gravity; |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 24 | import android.view.View; |
| 25 | import android.view.ViewGroup; |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 26 | import android.widget.LinearLayout; |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 27 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 28 | import com.android.contacts.R; |
| 29 | |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Layout similar to LinearLayout that allows a child to specify examples of |
| 32 | * desired size depending on the parent size. For example if the widget wants to |
| 33 | * be 100dip when parent is 200dip and 110dip when parent is 400dip, the layout |
| 34 | * will ensure these requirements and interpolate for other parent sizes. |
| 35 | * You can also specify minWidth for each child. You can have at most one |
| 36 | * child with layout_width="match_parent" - it will take the entire remaining |
| 37 | * space. |
| 38 | */ |
| 39 | public class InterpolatingLayout extends ViewGroup { |
| 40 | |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 41 | private Rect mInRect = new Rect(); |
| 42 | private Rect mOutRect = new Rect(); |
| 43 | |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 44 | public InterpolatingLayout(Context context) { |
| 45 | super(context); |
| 46 | } |
| 47 | |
| 48 | public InterpolatingLayout(Context context, AttributeSet attrs) { |
| 49 | super(context, attrs); |
| 50 | } |
| 51 | |
| 52 | public InterpolatingLayout(Context context, AttributeSet attrs, int defStyle) { |
| 53 | super(context, attrs, defStyle); |
| 54 | } |
| 55 | |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 56 | public final static class LayoutParams extends LinearLayout.LayoutParams { |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 57 | |
| 58 | public int narrowParentWidth; |
| 59 | public int narrowWidth; |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 60 | public int narrowMarginLeft; |
| 61 | public int narrowPaddingLeft; |
| 62 | public int narrowMarginRight; |
| 63 | public int narrowPaddingRight; |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 64 | public int wideParentWidth; |
| 65 | public int wideWidth; |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 66 | public int wideMarginLeft; |
| 67 | public int widePaddingLeft; |
| 68 | public int wideMarginRight; |
| 69 | public int widePaddingRight; |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 70 | private float widthMultiplier; |
| 71 | private int widthConstant; |
| 72 | private float leftMarginMultiplier; |
| 73 | private int leftMarginConstant; |
Dmitri Plotnikov | bb0788a | 2010-11-29 18:50:14 -0800 | [diff] [blame] | 74 | private float leftPaddingMultiplier; |
| 75 | private int leftPaddingConstant; |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 76 | private float rightMarginMultiplier; |
| 77 | private int rightMarginConstant; |
Dmitri Plotnikov | bb0788a | 2010-11-29 18:50:14 -0800 | [diff] [blame] | 78 | private float rightPaddingMultiplier; |
| 79 | private int rightPaddingConstant; |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 80 | |
| 81 | public LayoutParams(Context c, AttributeSet attrs) { |
| 82 | super(c, attrs); |
| 83 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.InterpolatingLayout_Layout); |
| 84 | |
| 85 | narrowParentWidth = a.getDimensionPixelSize( |
| 86 | R.styleable.InterpolatingLayout_Layout_layout_narrowParentWidth, -1); |
| 87 | narrowWidth = a.getDimensionPixelSize( |
| 88 | R.styleable.InterpolatingLayout_Layout_layout_narrowWidth, -1); |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 89 | narrowMarginLeft = a.getDimensionPixelSize( |
| 90 | R.styleable.InterpolatingLayout_Layout_layout_narrowMarginLeft, -1); |
| 91 | narrowPaddingLeft = a.getDimensionPixelSize( |
| 92 | R.styleable.InterpolatingLayout_Layout_layout_narrowPaddingLeft, -1); |
| 93 | narrowMarginRight = a.getDimensionPixelSize( |
| 94 | R.styleable.InterpolatingLayout_Layout_layout_narrowMarginRight, -1); |
| 95 | narrowPaddingRight = a.getDimensionPixelSize( |
| 96 | R.styleable.InterpolatingLayout_Layout_layout_narrowPaddingRight, -1); |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 97 | wideParentWidth = a.getDimensionPixelSize( |
| 98 | R.styleable.InterpolatingLayout_Layout_layout_wideParentWidth, -1); |
| 99 | wideWidth = a.getDimensionPixelSize( |
| 100 | R.styleable.InterpolatingLayout_Layout_layout_wideWidth, -1); |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 101 | wideMarginLeft = a.getDimensionPixelSize( |
| 102 | R.styleable.InterpolatingLayout_Layout_layout_wideMarginLeft, -1); |
| 103 | widePaddingLeft = a.getDimensionPixelSize( |
| 104 | R.styleable.InterpolatingLayout_Layout_layout_widePaddingLeft, -1); |
| 105 | wideMarginRight = a.getDimensionPixelSize( |
| 106 | R.styleable.InterpolatingLayout_Layout_layout_wideMarginRight, -1); |
| 107 | widePaddingRight = a.getDimensionPixelSize( |
| 108 | R.styleable.InterpolatingLayout_Layout_layout_widePaddingRight, -1); |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 109 | |
| 110 | a.recycle(); |
| 111 | |
| 112 | if (narrowWidth != -1) { |
| 113 | widthMultiplier = (float) (wideWidth - narrowWidth) |
| 114 | / (wideParentWidth - narrowParentWidth); |
| 115 | widthConstant = (int) (narrowWidth - narrowParentWidth * widthMultiplier); |
| 116 | } |
| 117 | |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 118 | if (narrowMarginLeft != -1) { |
| 119 | leftMarginMultiplier = (float) (wideMarginLeft - narrowMarginLeft) |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 120 | / (wideParentWidth - narrowParentWidth); |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 121 | leftMarginConstant = (int) (narrowMarginLeft - narrowParentWidth |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 122 | * leftMarginMultiplier); |
| 123 | } |
| 124 | |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 125 | if (narrowPaddingLeft != -1) { |
| 126 | leftPaddingMultiplier = (float) (widePaddingLeft - narrowPaddingLeft) |
Dmitri Plotnikov | bb0788a | 2010-11-29 18:50:14 -0800 | [diff] [blame] | 127 | / (wideParentWidth - narrowParentWidth); |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 128 | leftPaddingConstant = (int) (narrowPaddingLeft - narrowParentWidth |
Dmitri Plotnikov | bb0788a | 2010-11-29 18:50:14 -0800 | [diff] [blame] | 129 | * leftPaddingMultiplier); |
| 130 | } |
| 131 | |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 132 | if (narrowMarginRight != -1) { |
| 133 | rightMarginMultiplier = (float) (wideMarginRight - narrowMarginRight) |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 134 | / (wideParentWidth - narrowParentWidth); |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 135 | rightMarginConstant = (int) (narrowMarginRight - narrowParentWidth |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 136 | * rightMarginMultiplier); |
| 137 | } |
Dmitri Plotnikov | bb0788a | 2010-11-29 18:50:14 -0800 | [diff] [blame] | 138 | |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 139 | if (narrowPaddingRight != -1) { |
| 140 | rightPaddingMultiplier = (float) (widePaddingRight - narrowPaddingRight) |
Dmitri Plotnikov | bb0788a | 2010-11-29 18:50:14 -0800 | [diff] [blame] | 141 | / (wideParentWidth - narrowParentWidth); |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 142 | rightPaddingConstant = (int) (narrowPaddingRight - narrowParentWidth |
Dmitri Plotnikov | bb0788a | 2010-11-29 18:50:14 -0800 | [diff] [blame] | 143 | * rightPaddingMultiplier); |
| 144 | } |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | public LayoutParams(int width, int height) { |
| 148 | super(width, height); |
| 149 | } |
| 150 | |
| 151 | public LayoutParams(MarginLayoutParams source) { |
| 152 | super(source); |
| 153 | } |
| 154 | |
| 155 | public int resolveWidth(int parentSize) { |
| 156 | if (narrowWidth == -1) { |
| 157 | return width; |
| 158 | } else { |
| 159 | int w = (int) (parentSize * widthMultiplier) + widthConstant; |
| 160 | return w <= 0 ? WRAP_CONTENT : w; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | public int resolveLeftMargin(int parentSize) { |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 165 | if (narrowMarginLeft == -1) { |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 166 | return leftMargin; |
| 167 | } else { |
| 168 | int w = (int) (parentSize * leftMarginMultiplier) + leftMarginConstant; |
| 169 | return w < 0 ? 0 : w; |
| 170 | } |
| 171 | } |
| 172 | |
Dmitri Plotnikov | bb0788a | 2010-11-29 18:50:14 -0800 | [diff] [blame] | 173 | public int resolveLeftPadding(int parentSize) { |
| 174 | int w = (int) (parentSize * leftPaddingMultiplier) + leftPaddingConstant; |
| 175 | return w < 0 ? 0 : w; |
| 176 | } |
| 177 | |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 178 | public int resolveRightMargin(int parentSize) { |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 179 | if (narrowMarginRight == -1) { |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 180 | return rightMargin; |
| 181 | } else { |
| 182 | int w = (int) (parentSize * rightMarginMultiplier) + rightMarginConstant; |
| 183 | return w < 0 ? 0 : w; |
| 184 | } |
| 185 | } |
Dmitri Plotnikov | bb0788a | 2010-11-29 18:50:14 -0800 | [diff] [blame] | 186 | |
| 187 | public int resolveRightPadding(int parentSize) { |
| 188 | int w = (int) (parentSize * rightPaddingMultiplier) + rightPaddingConstant; |
| 189 | return w < 0 ? 0 : w; |
| 190 | } |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 195 | return new LayoutParams(getContext(), attrs); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | protected LayoutParams generateDefaultLayoutParams() { |
| 200 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 205 | int parentWidth = MeasureSpec.getSize(widthMeasureSpec); |
| 206 | int parentHeight = MeasureSpec.getSize(heightMeasureSpec); |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 207 | |
| 208 | int width = 0; |
| 209 | int height = 0; |
| 210 | |
| 211 | View fillChild = null; |
| 212 | int count = getChildCount(); |
| 213 | for (int i = 0; i < count; i++) { |
| 214 | View child = getChildAt(i); |
| 215 | if (child.getVisibility() == View.GONE) { |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | LayoutParams params = (LayoutParams) child.getLayoutParams(); |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 220 | if (params.width == LayoutParams.MATCH_PARENT) { |
| 221 | if (fillChild != null) { |
| 222 | throw new RuntimeException( |
| 223 | "Interpolating layout allows at most one child" |
| 224 | + " with layout_width='match_parent'"); |
| 225 | } |
| 226 | fillChild = child; |
| 227 | } else { |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 228 | int childWidth = params.resolveWidth(parentWidth); |
| 229 | int childWidthMeasureSpec; |
| 230 | switch (childWidth) { |
| 231 | case LayoutParams.WRAP_CONTENT: |
| 232 | childWidthMeasureSpec = MeasureSpec.UNSPECIFIED; |
| 233 | break; |
| 234 | default: |
| 235 | childWidthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 236 | childWidth, MeasureSpec.EXACTLY); |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | int childHeightMeasureSpec; |
| 241 | switch (params.height) { |
| 242 | case LayoutParams.WRAP_CONTENT: |
| 243 | childHeightMeasureSpec = MeasureSpec.UNSPECIFIED; |
| 244 | break; |
| 245 | case LayoutParams.MATCH_PARENT: |
| 246 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec( |
Dmitri Plotnikov | 65f8cae | 2010-12-10 16:38:22 -0800 | [diff] [blame] | 247 | parentHeight - params.topMargin - params.bottomMargin, |
| 248 | MeasureSpec.EXACTLY); |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 249 | break; |
| 250 | default: |
| 251 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 252 | params.height, MeasureSpec.EXACTLY); |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 257 | width += child.getMeasuredWidth(); |
| 258 | height = Math.max(child.getMeasuredHeight(), height); |
| 259 | } |
| 260 | |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 261 | width += params.resolveLeftMargin(parentWidth) + params.resolveRightMargin(parentWidth); |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | if (fillChild != null) { |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 265 | int remainder = parentWidth - width; |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 266 | int childMeasureSpec = remainder > 0 |
| 267 | ? MeasureSpec.makeMeasureSpec(remainder, MeasureSpec.EXACTLY) |
| 268 | : MeasureSpec.UNSPECIFIED; |
| 269 | fillChild.measure(childMeasureSpec, heightMeasureSpec); |
| 270 | width += fillChild.getMeasuredWidth(); |
| 271 | height = Math.max(fillChild.getMeasuredHeight(), height); |
| 272 | } |
| 273 | |
| 274 | setMeasuredDimension( |
| 275 | resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 280 | int offset = 0; |
| 281 | int width = right - left; |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 282 | int count = getChildCount(); |
| 283 | for (int i = 0; i < count; i++) { |
| 284 | View child = getChildAt(i); |
| 285 | |
Frank Sposaro | 76dd128 | 2011-06-24 11:09:04 -0700 | [diff] [blame] | 286 | if (child.getVisibility() == View.GONE) { |
| 287 | continue; |
| 288 | } |
| 289 | |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 290 | LayoutParams params = (LayoutParams) child.getLayoutParams(); |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 291 | int gravity = params.gravity; |
| 292 | if (gravity == -1) { |
Fabrice Di Meglio | 7d3b915 | 2013-04-04 19:49:34 -0700 | [diff] [blame] | 293 | gravity = Gravity.START | Gravity.TOP; |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 296 | if (params.narrowPaddingLeft != -1 || params.narrowPaddingRight != -1) { |
| 297 | int leftPadding = params.narrowPaddingLeft == -1 ? child.getPaddingLeft() |
Dmitri Plotnikov | bb0788a | 2010-11-29 18:50:14 -0800 | [diff] [blame] | 298 | : params.resolveLeftPadding(width); |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 299 | int rightPadding = params.narrowPaddingRight == -1 ? child.getPaddingRight() |
Dmitri Plotnikov | bb0788a | 2010-11-29 18:50:14 -0800 | [diff] [blame] | 300 | : params.resolveRightPadding(width); |
| 301 | child.setPadding( |
| 302 | leftPadding, child.getPaddingTop(), rightPadding, child.getPaddingBottom()); |
| 303 | } |
| 304 | |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 305 | int leftMargin = params.resolveLeftMargin(width); |
| 306 | int rightMargin = params.resolveRightMargin(width); |
| 307 | |
| 308 | mInRect.set(offset + leftMargin, params.topMargin, |
Dmitri Plotnikov | b18624f | 2011-01-21 19:10:43 -0800 | [diff] [blame] | 309 | right - rightMargin, bottom - params.bottomMargin); |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 310 | |
Dmitri Plotnikov | 75d6cae | 2011-01-11 11:00:17 -0800 | [diff] [blame] | 311 | Gravity.apply(gravity, child.getMeasuredWidth(), child.getMeasuredHeight(), |
Dmitri Plotnikov | 5c0935b | 2010-11-16 15:56:18 -0800 | [diff] [blame] | 312 | mInRect, mOutRect); |
| 313 | child.layout(mOutRect.left, mOutRect.top, mOutRect.right, mOutRect.bottom); |
| 314 | |
| 315 | offset = mOutRect.right + rightMargin; |
Dmitri Plotnikov | 2ddba01 | 2010-09-16 18:27:35 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |