Chih-Chung Chang | eb9b537 | 2009-09-10 12:36:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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.camera; |
| 18 | |
| 19 | import android.content.Context; |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 20 | import android.content.res.TypedArray; |
Chih-Chung Chang | eb9b537 | 2009-09-10 12:36:52 +0800 | [diff] [blame] | 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
| 24 | |
| 25 | // |
| 26 | // This is a layout which makes the children even spaced. |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 27 | // Currently it does not consider the padding parameters. |
Chih-Chung Chang | eb9b537 | 2009-09-10 12:36:52 +0800 | [diff] [blame] | 28 | // |
| 29 | public class EvenlySpacedLayout extends ViewGroup { |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 30 | private boolean mHorizontal; |
Chih-Chung Chang | eb9b537 | 2009-09-10 12:36:52 +0800 | [diff] [blame] | 31 | |
Owen Lin | f0b7d4b | 2009-09-22 03:35:52 +0800 | [diff] [blame] | 32 | // Wheather we keep the space in both ends of the layout |
| 33 | private boolean mKeepEndSpace; |
| 34 | |
Chih-Chung Chang | eb9b537 | 2009-09-10 12:36:52 +0800 | [diff] [blame] | 35 | public EvenlySpacedLayout(Context context, AttributeSet attrs) { |
| 36 | super(context, attrs); |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 37 | TypedArray a = context.obtainStyledAttributes( |
| 38 | attrs, R.styleable.EvenlySpacedLayout, 0, 0); |
| 39 | mHorizontal = (0 == a.getInt( |
| 40 | R.styleable.EvenlySpacedLayout_orientation, 0)); |
Owen Lin | f0b7d4b | 2009-09-22 03:35:52 +0800 | [diff] [blame] | 41 | mKeepEndSpace = a.getBoolean( |
| 42 | R.styleable.EvenlySpacedLayout_keepEndSpace, true); |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 43 | a.recycle(); |
Chih-Chung Chang | eb9b537 | 2009-09-10 12:36:52 +0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Override |
| 47 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 48 | int count = getChildCount(); |
| 49 | int width = 0; |
| 50 | int height = 0; |
| 51 | for (int i = 0; i < count; i++) { |
| 52 | View child = getChildAt(i); |
| 53 | if (child.getVisibility() == GONE) continue; |
| 54 | measureChild(child, widthMeasureSpec, heightMeasureSpec); |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 55 | if (mHorizontal) { |
| 56 | width += child.getMeasuredWidth(); |
| 57 | height = Math.max(height, child.getMeasuredHeight()); |
| 58 | } else { |
| 59 | height += child.getMeasuredHeight(); |
| 60 | width = Math.max(width, child.getMeasuredWidth()); |
| 61 | } |
Chih-Chung Chang | eb9b537 | 2009-09-10 12:36:52 +0800 | [diff] [blame] | 62 | } |
| 63 | setMeasuredDimension(resolveSize(width, widthMeasureSpec), |
| 64 | resolveSize(height, heightMeasureSpec)); |
| 65 | } |
| 66 | |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 67 | private void layoutHorizontal(boolean changed, int l, int t, int r, int b) { |
Chih-Chung Chang | eb9b537 | 2009-09-10 12:36:52 +0800 | [diff] [blame] | 68 | int count = getChildCount(); |
| 69 | |
| 70 | int usedWidth = 0; |
| 71 | int usedChildren = 0; |
| 72 | for (int i = 0; i < count; i++) { |
| 73 | View child = getChildAt(i); |
| 74 | if (child.getVisibility() == GONE) continue; |
| 75 | usedWidth += child.getMeasuredWidth(); |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 76 | ++usedChildren; |
Chih-Chung Chang | eb9b537 | 2009-09-10 12:36:52 +0800 | [diff] [blame] | 77 | } |
| 78 | |
Owen Lin | f0b7d4b | 2009-09-22 03:35:52 +0800 | [diff] [blame] | 79 | int spacing = (r - l - usedWidth) / |
| 80 | (mKeepEndSpace ? (usedChildren + 1) : (usedChildren - 1)); |
| 81 | int left = mKeepEndSpace ? spacing : 0; |
Chih-Chung Chang | eb9b537 | 2009-09-10 12:36:52 +0800 | [diff] [blame] | 82 | int top = 0; |
| 83 | for (int i = 0; i < count; i++) { |
| 84 | View child = getChildAt(i); |
| 85 | if (child.getVisibility() == GONE) continue; |
| 86 | int w = child.getMeasuredWidth(); |
| 87 | int h = child.getMeasuredHeight(); |
| 88 | child.layout(left, top, left + w, top + h); |
| 89 | left += w; |
| 90 | left += spacing; |
| 91 | } |
| 92 | } |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 93 | |
| 94 | private void layoutVertical(boolean changed, int l, int t, int r, int b) { |
| 95 | int count = getChildCount(); |
| 96 | |
| 97 | int usedHeight = 0; |
| 98 | int usedChildren = 0; |
| 99 | for (int i = 0; i < count; i++) { |
| 100 | View child = getChildAt(i); |
| 101 | if (child.getVisibility() == GONE) continue; |
| 102 | usedHeight += child.getMeasuredHeight(); |
| 103 | ++usedChildren; |
| 104 | } |
| 105 | |
Owen Lin | f0b7d4b | 2009-09-22 03:35:52 +0800 | [diff] [blame] | 106 | int spacing = (b - t - usedHeight) / |
| 107 | (mKeepEndSpace ? (usedChildren + 1) : (usedChildren - 1)); |
| 108 | int top = mKeepEndSpace ? spacing : 0; |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 109 | int left = 0; |
| 110 | for (int i = 0; i < count; i++) { |
| 111 | View child = getChildAt(i); |
| 112 | if (child.getVisibility() == GONE) continue; |
| 113 | int w = child.getMeasuredWidth(); |
| 114 | int h = child.getMeasuredHeight(); |
| 115 | child.layout(left, top, left + w, top + h); |
| 116 | top += h; |
| 117 | top += spacing; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 123 | if (mHorizontal) { |
| 124 | layoutHorizontal(changed, l, t, r, b); |
| 125 | } else { |
| 126 | layoutVertical(changed, l, t, r, b); |
| 127 | } |
| 128 | } |
Chih-Chung Chang | eb9b537 | 2009-09-10 12:36:52 +0800 | [diff] [blame] | 129 | } |