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.camera.R; 20 21 import android.content.Context; 22 import android.graphics.Rect; 23 import android.util.AttributeSet; 24 import android.widget.RelativeLayout; 25 /** 26 * A layout which handles the preview aspect ratio. 27 */ 28 public class PreviewFrameLayout extends RelativeLayout { 29 /** A callback to be invoked when the preview frame's size changes. */ 30 public interface OnSizeChangedListener { 31 public void onSizeChanged(); 32 } 33 34 private double mAspectRatio = 4.0 / 3.0; 35 36 public PreviewFrameLayout(Context context, AttributeSet attrs) { 37 super(context, attrs); 38 } 39 40 public void setAspectRatio(double ratio) { 41 if (ratio <= 0.0) throw new IllegalArgumentException(); 42 if (mAspectRatio != ratio) { 43 mAspectRatio = ratio; 44 requestLayout(); 45 } 46 } 47 48 public void showBorder(boolean enabled) { 49 setActivated(enabled); 50 } 51 52 53 @Override 54 protected void onMeasure(int widthSpec, int heightSpec) { 55 int previewWidth = MeasureSpec.getSize(widthSpec); 56 int previewHeight = MeasureSpec.getSize(heightSpec); 57 58 // Get the padding of the border background. 59 int hPadding = mPaddingLeft + mPaddingRight; 60 int vPadding = mPaddingTop + mPaddingBottom; 61 62 // Resize the preview frame with correct aspect ratio. 63 previewWidth -= hPadding; 64 previewHeight -= vPadding; 65 if (previewWidth > previewHeight * mAspectRatio) { 66 previewWidth = (int) (previewHeight * mAspectRatio + .5); 67 } else { 68 previewHeight = (int) (previewWidth / mAspectRatio + .5); 69 } 70 71 // Add the padding of the border. 72 previewWidth += hPadding; 73 previewHeight += vPadding; 74 75 // Ask children to follow the new preview dimension. 76 super.onMeasure(MeasureSpec.makeMeasureSpec(previewWidth, MeasureSpec.EXACTLY), 77 MeasureSpec.makeMeasureSpec(previewHeight, MeasureSpec.EXACTLY)); 78 } 79 } 80