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