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