2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera;
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;
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;
32 * A layout which handles the preview aspect ratio.
34 public class PreviewFrameLayout extends RelativeLayout implements LayoutChangeNotifier {
36 private static final String TAG = "CAM_preview";
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);
43 private double mAspectRatio;
45 private OnSizeChangedListener mListener;
46 private LayoutChangeHelper mLayoutChangeHelper;
48 public PreviewFrameLayout(Context context, AttributeSet attrs) {
49 super(context, attrs);
50 setAspectRatio(4.0 / 3.0);
51 mLayoutChangeHelper = new LayoutChangeHelper(this);
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
62 if (faceViewStub != null) {
63 faceViewStub.inflate();
68 public void setAspectRatio(double ratio) {
69 if (ratio <= 0.0) throw new IllegalArgumentException();
71 if (mAspectRatio != ratio) {
77 public void showBorder(boolean enabled) {
78 mBorder.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
81 public void fadeOutBorder() {
82 Util.fadeOut(mBorder);
86 protected void onMeasure(int widthSpec, int heightSpec) {
87 int previewWidth = MeasureSpec.getSize(widthSpec);
88 int previewHeight = MeasureSpec.getSize(heightSpec);
90 if (!ApiHelper.HAS_SURFACE_TEXTURE) {
91 // Get the padding of the border background.
92 int hPadding = getPaddingLeft() + getPaddingRight();
93 int vPadding = getPaddingTop() + getPaddingBottom();
95 // Resize the preview frame with correct aspect ratio.
96 previewWidth -= hPadding;
97 previewHeight -= vPadding;
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);
105 shortSide = (int) ((double) longSide / mAspectRatio);
108 previewWidth = longSide;
109 previewHeight = shortSide;
111 previewWidth = shortSide;
112 previewHeight = longSide;
115 // Add the padding of the border.
116 previewWidth += hPadding;
117 previewHeight += vPadding;
120 // Ask children to follow the new preview dimension.
121 super.onMeasure(MeasureSpec.makeMeasureSpec(previewWidth, MeasureSpec.EXACTLY),
122 MeasureSpec.makeMeasureSpec(previewHeight, MeasureSpec.EXACTLY));
125 public void setOnSizeChangedListener(OnSizeChangedListener listener) {
126 mListener = listener;
130 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
131 if (mListener != null) mListener.onSizeChanged(w, h);
135 public void setOnLayoutChangeListener(
136 LayoutChangeNotifier.Listener listener) {
137 mLayoutChangeHelper.setOnLayoutChangeListener(listener);
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);