OSDN Git Service

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