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