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.camera.ui; 18 19 import android.graphics.Rect; 20 import android.view.View.MeasureSpec; 21 22 public class MeasureHelper { 23 24 private final GLView mComponent; 25 private int mPreferredWidth; 26 private int mPreferredHeight; 27 28 public MeasureHelper(GLView component) { 29 mComponent = component; 30 } 31 32 public MeasureHelper setPreferredContentSize(int width, int height) { 33 mPreferredWidth = width; 34 mPreferredHeight = height; 35 return this; 36 } 37 38 public void measure(int widthSpec, int heightSpec) { 39 Rect p = mComponent.getPaddings(); 40 setMeasuredSize( 41 getLength(widthSpec, mPreferredWidth + p.left + p.right), 42 getLength(heightSpec, mPreferredHeight + p.top + p.bottom)); 43 } 44 45 private static int getLength(int measureSpec, int prefered) { 46 int specLength = MeasureSpec.getSize(measureSpec); 47 switch(MeasureSpec.getMode(measureSpec)) { 48 case MeasureSpec.EXACTLY: return specLength; 49 case MeasureSpec.AT_MOST: return Math.min(prefered, specLength); 50 default: return prefered; 51 } 52 } 53 54 protected void setMeasuredSize(int width, int height) { 55 mComponent.setMeasuredSize(width, height); 56 } 57 58 } 59