blob: d994fe42c5e647f44d1128ef3c86531f421fbd6d [file] [log] [blame]
Chih-Chung Changeb9b5372009-09-10 12:36:52 +08001/*
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
17package com.android.camera;
18
19import android.content.Context;
Owen Lin37acf792009-09-17 17:55:37 +080020import android.content.res.TypedArray;
Chih-Chung Changeb9b5372009-09-10 12:36:52 +080021import android.util.AttributeSet;
22import android.view.View;
23import android.view.ViewGroup;
24
25//
26// This is a layout which makes the children even spaced.
Owen Lin37acf792009-09-17 17:55:37 +080027// Currently it does not consider the padding parameters.
Chih-Chung Changeb9b5372009-09-10 12:36:52 +080028//
29public class EvenlySpacedLayout extends ViewGroup {
Owen Lin37acf792009-09-17 17:55:37 +080030 private boolean mHorizontal;
Chih-Chung Changeb9b5372009-09-10 12:36:52 +080031
Owen Linf0b7d4b2009-09-22 03:35:52 +080032 // Wheather we keep the space in both ends of the layout
33 private boolean mKeepEndSpace;
34
Chih-Chung Changeb9b5372009-09-10 12:36:52 +080035 public EvenlySpacedLayout(Context context, AttributeSet attrs) {
36 super(context, attrs);
Owen Lin37acf792009-09-17 17:55:37 +080037 TypedArray a = context.obtainStyledAttributes(
38 attrs, R.styleable.EvenlySpacedLayout, 0, 0);
39 mHorizontal = (0 == a.getInt(
40 R.styleable.EvenlySpacedLayout_orientation, 0));
Owen Linf0b7d4b2009-09-22 03:35:52 +080041 mKeepEndSpace = a.getBoolean(
42 R.styleable.EvenlySpacedLayout_keepEndSpace, true);
Owen Lin37acf792009-09-17 17:55:37 +080043 a.recycle();
Chih-Chung Changeb9b5372009-09-10 12:36:52 +080044 }
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 Lin37acf792009-09-17 17:55:37 +080055 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 Changeb9b5372009-09-10 12:36:52 +080062 }
63 setMeasuredDimension(resolveSize(width, widthMeasureSpec),
64 resolveSize(height, heightMeasureSpec));
65 }
66
Owen Lin37acf792009-09-17 17:55:37 +080067 private void layoutHorizontal(boolean changed, int l, int t, int r, int b) {
Chih-Chung Changeb9b5372009-09-10 12:36:52 +080068 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 Lin37acf792009-09-17 17:55:37 +080076 ++usedChildren;
Chih-Chung Changeb9b5372009-09-10 12:36:52 +080077 }
78
Owen Linf0b7d4b2009-09-22 03:35:52 +080079 int spacing = (r - l - usedWidth) /
80 (mKeepEndSpace ? (usedChildren + 1) : (usedChildren - 1));
81 int left = mKeepEndSpace ? spacing : 0;
Chih-Chung Changeb9b5372009-09-10 12:36:52 +080082 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 Lin37acf792009-09-17 17:55:37 +080093
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 Linf0b7d4b2009-09-22 03:35:52 +0800106 int spacing = (b - t - usedHeight) /
107 (mKeepEndSpace ? (usedChildren + 1) : (usedChildren - 1));
108 int top = mKeepEndSpace ? spacing : 0;
Owen Lin37acf792009-09-17 17:55:37 +0800109 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 Changeb9b5372009-09-10 12:36:52 +0800129}