Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2011 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 package com.android.browser.view;
     17 
     18 import android.content.Context;
     19 import android.util.AttributeSet;
     20 import android.widget.GridView;
     21 
     22 public class SnapshotGridView extends GridView {
     23 
     24     private static final int MAX_COLUMNS = 5;
     25 
     26     private int mColWidth;
     27 
     28     public SnapshotGridView(Context context) {
     29         super(context);
     30     }
     31 
     32     public SnapshotGridView(Context context, AttributeSet attrs) {
     33         super(context, attrs);
     34     }
     35 
     36     public SnapshotGridView(Context context, AttributeSet attrs, int defStyle) {
     37         super(context, attrs, defStyle);
     38     }
     39 
     40     @Override
     41     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     42         int widthSize = MeasureSpec.getSize(widthMeasureSpec);
     43         int widthMode = MeasureSpec.getMode(widthMeasureSpec);
     44         if (widthSize > 0 && mColWidth > 0) {
     45             int numCols = widthSize / mColWidth;
     46             widthSize = Math.min(
     47                     Math.min(numCols, MAX_COLUMNS) * mColWidth,
     48                     widthSize);
     49             widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
     50         }
     51         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     52     }
     53 
     54     @Override
     55     public void setColumnWidth(int columnWidth) {
     56         mColWidth = columnWidth;
     57         super.setColumnWidth(columnWidth);
     58     }
     59 }
     60