• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.gallery3d.ui;
18 
19 import android.graphics.Rect;
20 import android.view.View.MeasureSpec;
21 
22 class MeasureHelper {
23 
24     private static MeasureHelper sInstance = new MeasureHelper(null);
25 
26     private GLView mComponent;
27     private int mPreferredWidth;
28     private int mPreferredHeight;
29 
MeasureHelper(GLView component)30     private MeasureHelper(GLView component) {
31         mComponent = component;
32     }
33 
getInstance(GLView component)34     public static MeasureHelper getInstance(GLView component) {
35         sInstance.mComponent = component;
36         return sInstance;
37     }
38 
setPreferredContentSize(int width, int height)39     public MeasureHelper setPreferredContentSize(int width, int height) {
40         mPreferredWidth = width;
41         mPreferredHeight = height;
42         return this;
43     }
44 
measure(int widthSpec, int heightSpec)45     public void measure(int widthSpec, int heightSpec) {
46         Rect p = mComponent.getPaddings();
47         setMeasuredSize(
48                 getLength(widthSpec, mPreferredWidth + p.left + p.right),
49                 getLength(heightSpec, mPreferredHeight + p.top + p.bottom));
50     }
51 
getLength(int measureSpec, int prefered)52     private static int getLength(int measureSpec, int prefered) {
53         int specLength = MeasureSpec.getSize(measureSpec);
54         switch(MeasureSpec.getMode(measureSpec)) {
55             case MeasureSpec.EXACTLY: return specLength;
56             case MeasureSpec.AT_MOST: return Math.min(prefered, specLength);
57             default: return prefered;
58         }
59     }
60 
setMeasuredSize(int width, int height)61     protected void setMeasuredSize(int width, int height) {
62         mComponent.setMeasuredSize(width, height);
63     }
64 
65 }
66