OSDN Git Service

451a35ad3f7ade74edc41a67f14fd01d9ed3d7cb
[android-x86/packages-apps-Gallery2.git] / src / com / android / camera / PreviewFrameLayout.java
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 android.content.Context;
20 import android.content.res.Configuration;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.view.ViewStub;
24 import android.widget.RelativeLayout;
25
26 import com.android.camera.ui.LayoutChangeHelper;
27 import com.android.camera.ui.LayoutChangeNotifier;
28 import com.android.gallery3d.common.ApiHelper;
29
30 /**
31  * A layout which handles the preview aspect ratio.
32  */
33 public class PreviewFrameLayout extends RelativeLayout implements LayoutChangeNotifier {
34
35     private static final String TAG = "CAM_preview";
36
37     /** A callback to be invoked when the preview frame's size changes. */
38     public interface OnSizeChangedListener {
39         public void onSizeChanged(int width, int height);
40     }
41
42     private double mAspectRatio;
43     private View mBorder;
44     private OnSizeChangedListener mListener;
45     private LayoutChangeHelper mLayoutChangeHelper;
46
47     public PreviewFrameLayout(Context context, AttributeSet attrs) {
48         super(context, attrs);
49         setAspectRatio(4.0 / 3.0);
50         mLayoutChangeHelper = new LayoutChangeHelper(this);
51     }
52
53     @Override
54     protected void onFinishInflate() {
55         mBorder = findViewById(R.id.preview_border);
56         if (ApiHelper.HAS_FACE_DETECTION) {
57             ViewStub faceViewStub = (ViewStub) findViewById(R.id.face_view_stub);
58             /* preview_frame_video.xml does not have face view stub, so we need to
59              * check that.
60              */
61             if (faceViewStub != null) {
62                 faceViewStub.inflate();
63             }
64         }
65     }
66
67     public void setAspectRatio(double ratio) {
68         if (ratio <= 0.0) throw new IllegalArgumentException();
69
70         if (mAspectRatio != ratio) {
71             mAspectRatio = ratio;
72             requestLayout();
73         }
74     }
75
76     public void showBorder(boolean enabled) {
77         mBorder.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
78     }
79
80     public void fadeOutBorder() {
81         Util.fadeOut(mBorder);
82     }
83
84     @Override
85     protected void onMeasure(int widthSpec, int heightSpec) {
86         int previewWidth = MeasureSpec.getSize(widthSpec);
87         int previewHeight = MeasureSpec.getSize(heightSpec);
88
89         if (!ApiHelper.HAS_SURFACE_TEXTURE) {
90             // Get the padding of the border background.
91             int hPadding = getPaddingLeft() + getPaddingRight();
92             int vPadding = getPaddingTop() + getPaddingBottom();
93
94             // Resize the preview frame with correct aspect ratio.
95             previewWidth -= hPadding;
96             previewHeight -= vPadding;
97
98             boolean widthLonger = previewWidth > previewHeight;
99             int longSide = (widthLonger ? previewWidth : previewHeight);
100             int shortSide = (widthLonger ? previewHeight : previewWidth);
101             if (longSide > shortSide * mAspectRatio) {
102                 longSide = (int) ((double) shortSide * mAspectRatio);
103             } else {
104                 shortSide = (int) ((double) longSide / mAspectRatio);
105             }
106             if (widthLonger) {
107                 previewWidth = longSide;
108                 previewHeight = shortSide;
109             } else {
110                 previewWidth = shortSide;
111                 previewHeight = longSide;
112             }
113
114             // Add the padding of the border.
115             previewWidth += hPadding;
116             previewHeight += vPadding;
117         }
118
119         // Ask children to follow the new preview dimension.
120         super.onMeasure(MeasureSpec.makeMeasureSpec(previewWidth, MeasureSpec.EXACTLY),
121                 MeasureSpec.makeMeasureSpec(previewHeight, MeasureSpec.EXACTLY));
122     }
123
124     public void setOnSizeChangedListener(OnSizeChangedListener listener) {
125         mListener = listener;
126     }
127
128     @Override
129     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
130         if (mListener != null) mListener.onSizeChanged(w, h);
131     }
132
133     @Override
134     public void setOnLayoutChangeListener(
135             LayoutChangeNotifier.Listener listener) {
136         mLayoutChangeHelper.setOnLayoutChangeListener(listener);
137     }
138
139     @Override
140     protected void onLayout(boolean changed, int l, int t, int r, int b) {
141         super.onLayout(changed, l, t, r, b);
142         mLayoutChangeHelper.onLayout(changed, l, t, r, b);
143     }
144 }