PreviewFrameLayout.java revision 892781804513066b68b4a2fa6f846e862dac252a
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.app.Activity;
20import android.content.Context;
21import android.content.pm.ActivityInfo;
22import android.util.AttributeSet;
23import android.view.View;
24import android.widget.RelativeLayout;
25
26/**
27 * A layout which handles the preview aspect ratio.
28 */
29public class PreviewFrameLayout extends RelativeLayout {
30    /** A callback to be invoked when the preview frame's size changes. */
31    public interface OnSizeChangedListener {
32        public void onSizeChanged();
33    }
34
35    private double mAspectRatio;
36    private View mBorder;
37
38    public PreviewFrameLayout(Context context, AttributeSet attrs) {
39        super(context, attrs);
40        setAspectRatio(4.0 / 3.0);
41    }
42
43    @Override
44    protected void onFinishInflate() {
45        mBorder = findViewById(R.id.preview_border);
46    }
47
48    public void setAspectRatio(double ratio) {
49        if (ratio <= 0.0) throw new IllegalArgumentException();
50
51        if (((Activity) getContext()).getRequestedOrientation()
52                == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
53            ratio = 1 / ratio;
54        }
55
56        if (mAspectRatio != ratio) {
57            mAspectRatio = ratio;
58            requestLayout();
59        }
60    }
61
62    public void showBorder(boolean enabled) {
63        mBorder.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
64    }
65
66    public void fadeOutBorder() {
67        Util.fadeOut(mBorder);
68    }
69
70    @Override
71    protected void onMeasure(int widthSpec, int heightSpec) {
72        int previewWidth = MeasureSpec.getSize(widthSpec);
73        int previewHeight = MeasureSpec.getSize(heightSpec);
74
75        // Get the padding of the border background.
76        int hPadding = mPaddingLeft + mPaddingRight;
77        int vPadding = mPaddingTop + mPaddingBottom;
78
79        // Resize the preview frame with correct aspect ratio.
80        previewWidth -= hPadding;
81        previewHeight -= vPadding;
82        if (previewWidth > previewHeight * mAspectRatio) {
83            previewWidth = (int) (previewHeight * mAspectRatio + .5);
84        } else {
85            previewHeight = (int) (previewWidth / mAspectRatio + .5);
86        }
87
88        // Add the padding of the border.
89        previewWidth += hPadding;
90        previewHeight += vPadding;
91
92        // Ask children to follow the new preview dimension.
93        super.onMeasure(MeasureSpec.makeMeasureSpec(previewWidth, MeasureSpec.EXACTLY),
94                MeasureSpec.makeMeasureSpec(previewHeight, MeasureSpec.EXACTLY));
95    }
96}
97