PreviewFrameLayout.java revision a2cbd31e81bf6b4a65ade4a5c34c303546d57d8c
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.util.AttributeSet;
22import android.util.DisplayMetrics;
23import android.view.View;
24import android.view.ViewGroup;
25
26/**
27 * A layout which handles the preview aspect ratio.
28 */
29public class PreviewFrameLayout extends ViewGroup {
30    private static final int MIN_HORIZONTAL_MARGIN = 10; // 10dp
31
32    /** A callback to be invoked when the preview frame's size changes. */
33    public interface OnSizeChangedListener {
34        public void onSizeChanged();
35    }
36
37    private double mAspectRatio = 4.0 / 3.0;
38    private View mFrame;
39    private OnSizeChangedListener mSizeListener;
40    private final DisplayMetrics mMetrics = new DisplayMetrics();
41
42    public PreviewFrameLayout(Context context, AttributeSet attrs) {
43        super(context, attrs);
44        ((Activity) context).getWindowManager()
45                .getDefaultDisplay().getMetrics(mMetrics);
46    }
47
48    public void setOnSizeChangedListener(OnSizeChangedListener listener) {
49        mSizeListener = listener;
50    }
51
52    @Override
53    protected void onFinishInflate() {
54        mFrame = (View) findViewById(R.id.frame);
55        if (mFrame == null) {
56            throw new IllegalStateException(
57                    "must provide child with id as \"frame\"");
58        }
59    }
60
61    public void setAspectRatio(double ratio) {
62        if (ratio <= 0.0) throw new IllegalArgumentException();
63
64        if (mAspectRatio != ratio) {
65            mAspectRatio = ratio;
66            requestLayout();
67        }
68    }
69
70    @Override
71    protected void onLayout(boolean changed, int l, int t, int r, int b) {
72        int frameWidth = getWidth();
73        int frameHeight = getHeight();
74
75        View f = mFrame;
76        int horizontalPadding = f.getPaddingLeft() + f.getPaddingRight();
77        int verticalPadding = f.getPaddingBottom() + f.getPaddingTop();
78        int previewHeight = frameHeight - verticalPadding;
79        int previewWidth = frameWidth - horizontalPadding;
80
81        // resize frame and preview for aspect ratio
82        if (previewWidth > previewHeight * mAspectRatio) {
83            previewWidth = (int) (previewHeight * mAspectRatio + .5);
84        } else {
85            previewHeight = (int) (previewWidth / mAspectRatio + .5);
86        }
87
88        frameWidth = previewWidth + horizontalPadding;
89        frameHeight = previewHeight + verticalPadding;
90
91        int hSpace = ((r - l) - frameWidth) / 2;
92        int vSpace = ((b - t) - frameHeight) / 2;
93        mFrame.measure(
94                MeasureSpec.makeMeasureSpec(frameWidth, MeasureSpec.EXACTLY),
95                MeasureSpec.makeMeasureSpec(frameHeight, MeasureSpec.EXACTLY));
96        mFrame.layout(hSpace, vSpace, frameWidth + hSpace, frameHeight + vSpace);
97        if (mSizeListener != null) {
98            mSizeListener.onSizeChanged();
99        }
100    }
101}
102
103