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.common.ApiHelper;
31 * A layout which handles the preview aspect ratio.
33 public class PreviewFrameLayout extends RelativeLayout implements LayoutChangeNotifier {
35 private static final String TAG = "CAM_preview";
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);
42 private double mAspectRatio;
44 private OnSizeChangedListener mListener;
45 private LayoutChangeHelper mLayoutChangeHelper;
47 public PreviewFrameLayout(Context context, AttributeSet attrs) {
48 super(context, attrs);
49 setAspectRatio(4.0 / 3.0);
50 mLayoutChangeHelper = new LayoutChangeHelper(this);
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
61 if (faceViewStub != null) {
62 faceViewStub.inflate();
67 public void setAspectRatio(double ratio) {
68 if (ratio <= 0.0) throw new IllegalArgumentException();
70 if (mAspectRatio != ratio) {
76 public void showBorder(boolean enabled) {
77 mBorder.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
80 public void fadeOutBorder() {
81 Util.fadeOut(mBorder);
85 protected void onMeasure(int widthSpec, int heightSpec) {
86 int previewWidth = MeasureSpec.getSize(widthSpec);
87 int previewHeight = MeasureSpec.getSize(heightSpec);
89 if (!ApiHelper.HAS_SURFACE_TEXTURE) {
90 // Get the padding of the border background.
91 int hPadding = getPaddingLeft() + getPaddingRight();
92 int vPadding = getPaddingTop() + getPaddingBottom();
94 // Resize the preview frame with correct aspect ratio.
95 previewWidth -= hPadding;
96 previewHeight -= vPadding;
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);
104 shortSide = (int) ((double) longSide / mAspectRatio);
107 previewWidth = longSide;
108 previewHeight = shortSide;
110 previewWidth = shortSide;
111 previewHeight = longSide;
114 // Add the padding of the border.
115 previewWidth += hPadding;
116 previewHeight += vPadding;
119 // Ask children to follow the new preview dimension.
120 super.onMeasure(MeasureSpec.makeMeasureSpec(previewWidth, MeasureSpec.EXACTLY),
121 MeasureSpec.makeMeasureSpec(previewHeight, MeasureSpec.EXACTLY));
124 public void setOnSizeChangedListener(OnSizeChangedListener listener) {
125 mListener = listener;
129 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
130 if (mListener != null) mListener.onSizeChanged(w, h);
134 public void setOnLayoutChangeListener(
135 LayoutChangeNotifier.Listener listener) {
136 mLayoutChangeHelper.setOnLayoutChangeListener(listener);
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);