Home | History | Annotate | Download | only in animation
      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.example.android.apis.animation;
     18 
     19 import android.content.Context;
     20 import android.content.res.TypedArray;
     21 import android.util.AttributeSet;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 
     25 /**
     26  * A layout that arranges its children in a grid.  The size of the
     27  * cells is set by the {@link #setCellSize} method and the
     28  * android:cell_width and android:cell_height attributes in XML.
     29  * The number of rows and columns is determined at runtime.  Each
     30  * cell contains exactly one view, and they flow in the natural
     31  * child order (the order in which they were added, or the index
     32  * in {@link #addViewAt}.  Views can not span multiple cells.
     33  *
     34  * <p>This class was copied from the FixedGridLayout Api demo; see that demo for
     35  * more information on using the layout.</p>
     36  */
     37 public class FixedGridLayout extends ViewGroup {
     38     int mCellWidth;
     39     int mCellHeight;
     40 
     41     public FixedGridLayout(Context context) {
     42         super(context);
     43     }
     44 
     45     public void setCellWidth(int px) {
     46         mCellWidth = px;
     47         requestLayout();
     48     }
     49 
     50     public void setCellHeight(int px) {
     51         mCellHeight = px;
     52         requestLayout();
     53     }
     54 
     55     @Override
     56     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     57         int cellWidthSpec = MeasureSpec.makeMeasureSpec(mCellWidth,
     58                 MeasureSpec.AT_MOST);
     59         int cellHeightSpec = MeasureSpec.makeMeasureSpec(mCellHeight,
     60                 MeasureSpec.AT_MOST);
     61 
     62         int count = getChildCount();
     63         for (int index=0; index<count; index++) {
     64             final View child = getChildAt(index);
     65             child.measure(cellWidthSpec, cellHeightSpec);
     66         }
     67         // Use the size our parents gave us, but default to a minimum size to avoid
     68         // clipping transitioning children
     69         int minCount =  count > 3 ? count : 3;
     70         setMeasuredDimension(resolveSize(mCellWidth * minCount, widthMeasureSpec),
     71                 resolveSize(mCellHeight * minCount, heightMeasureSpec));
     72     }
     73 
     74     @Override
     75     protected void onLayout(boolean changed, int l, int t, int r, int b) {
     76         int cellWidth = mCellWidth;
     77         int cellHeight = mCellHeight;
     78         int columns = (r - l) / cellWidth;
     79         if (columns < 0) {
     80             columns = 1;
     81         }
     82         int x = 0;
     83         int y = 0;
     84         int i = 0;
     85         int count = getChildCount();
     86         for (int index=0; index<count; index++) {
     87             final View child = getChildAt(index);
     88 
     89             int w = child.getMeasuredWidth();
     90             int h = child.getMeasuredHeight();
     91 
     92             int left = x + ((cellWidth-w)/2);
     93             int top = y + ((cellHeight-h)/2);
     94 
     95             child.layout(left, top, left+w, top+h);
     96             if (i >= (columns-1)) {
     97                 // advance to next row
     98                 i = 0;
     99                 x = 0;
    100                 y += cellHeight;
    101             } else {
    102                 i++;
    103                 x += cellWidth;
    104             }
    105         }
    106     }
    107 }
    108 
    109