PreviewFrameLayout.java revision 29c6b08eea023aa834e66802b6171b8d33b2b7d8
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            faceViewStub.inflate();
56        }
57    }
58
59    public void setAspectRatio(double ratio) {
60        if (ratio <= 0.0) throw new IllegalArgumentException();
61
62        if (getResources().getConfiguration().orientation
63                == Configuration.ORIENTATION_PORTRAIT) {
64            ratio = 1 / ratio;
65        }
66
67        if (mAspectRatio != ratio) {
68            mAspectRatio = ratio;
69            requestLayout();
70        }
71    }
72
73    public void showBorder(boolean enabled) {
74        mBorder.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
75    }
76
77    public void fadeOutBorder() {
78        Util.fadeOut(mBorder);
79    }
80
81    @Override
82    protected void onMeasure(int widthSpec, int heightSpec) {
83        int previewWidth = MeasureSpec.getSize(widthSpec);
84        int previewHeight = MeasureSpec.getSize(heightSpec);
85
86        // Get the padding of the border background.
87        int hPadding = getPaddingLeft() + getPaddingRight();
88        int vPadding = getPaddingTop() + getPaddingBottom();
89
90        // Resize the preview frame with correct aspect ratio.
91        previewWidth -= hPadding;
92        previewHeight -= vPadding;
93        if (previewWidth > previewHeight * mAspectRatio) {
94            previewWidth = (int) (previewHeight * mAspectRatio + .5);
95        } else {
96            previewHeight = (int) (previewWidth / mAspectRatio + .5);
97        }
98
99        // Add the padding of the border.
100        previewWidth += hPadding;
101        previewHeight += vPadding;
102
103        // Ask children to follow the new preview dimension.
104        super.onMeasure(MeasureSpec.makeMeasureSpec(previewWidth, MeasureSpec.EXACTLY),
105                MeasureSpec.makeMeasureSpec(previewHeight, MeasureSpec.EXACTLY));
106    }
107
108    public void setOnSizeChangedListener(OnSizeChangedListener listener) {
109        mListener = listener;
110    }
111
112    @Override
113    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
114        if (mListener != null) mListener.onSizeChanged(w, h);
115    }
116
117    @Override
118    public void setOnLayoutChangeListener(
119            LayoutChangeNotifier.Listener listener) {
120        mLayoutChangeHelper.setOnLayoutChangeListener(listener);
121    }
122
123    @Override
124    protected void onLayout(boolean changed, int l, int t, int r, int b) {
125        super.onLayout(changed, l, t, r, b);
126        mLayoutChangeHelper.onLayout(changed, l, t, r, b);
127    }
128}
129