PreviewFrameLayout.java revision 82544b4564c079ece1a9065d19add36b2635bb8f
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.ViewGroup;
24import android.widget.FrameLayout;
25
26/**
27 * A layout which handles the preview aspect ratio and the position of
28 * the gripper.
29 */
30public class PreviewFrameLayout extends ViewGroup {
31    private static final int MIN_HORIZONTAL_MARGIN = 10; // 10dp
32
33    /** A callback to be invoked when the preview frame's size changes. */
34    public interface OnSizeChangedListener {
35        public void onSizeChanged();
36    }
37
38    private double mAspectRatio = 4.0 / 3.0;
39    private FrameLayout mFrame;
40    private OnSizeChangedListener mSizeListener;
41    private final DisplayMetrics mMetrics = new DisplayMetrics();
42
43    public PreviewFrameLayout(Context context, AttributeSet attrs) {
44        super(context, attrs);
45        ((Activity) context).getWindowManager()
46                .getDefaultDisplay().getMetrics(mMetrics);
47    }
48
49    public void setOnSizeChangedListener(OnSizeChangedListener listener) {
50        mSizeListener = listener;
51    }
52
53    @Override
54    protected void onFinishInflate() {
55        mFrame = (FrameLayout) findViewById(R.id.frame);
56        if (mFrame == null) {
57            throw new IllegalStateException(
58                    "must provide child with id as \"frame\"");
59        }
60    }
61
62    public void setAspectRatio(double ratio) {
63        if (ratio <= 0.0) throw new IllegalArgumentException();
64
65        if (mAspectRatio != ratio) {
66            mAspectRatio = ratio;
67            requestLayout();
68        }
69    }
70
71    @Override
72    protected void onLayout(boolean changed, int l, int t, int r, int b) {
73        // Try to layout the "frame" in the center of the area, and put
74        // "gripper" just to the left of it. If there is no enough space for
75        // the gripper, the "frame" will be moved a little right so that
76        // they won't overlap with each other.
77
78        int frameWidth = getWidth();
79        int frameHeight = getHeight();
80
81        FrameLayout f = mFrame;
82
83        int horizontalPadding = Math.max(
84                f.getPaddingLeft() + f.getPaddingRight(),
85                (int) (MIN_HORIZONTAL_MARGIN * mMetrics.density));
86        int verticalPadding = f.getPaddingBottom() + f.getPaddingTop();
87
88        // Ignore the vertical paddings, so that we won't draw the frame on the
89        // top and bottom sides
90        int previewHeight = frameHeight;
91        int previewWidth = frameWidth - horizontalPadding;
92
93        // resize frame and preview for aspect ratio
94        if (previewWidth > previewHeight * mAspectRatio) {
95            previewWidth = (int) (previewHeight * mAspectRatio + .5);
96        } else {
97            previewHeight = (int) (previewWidth / mAspectRatio + .5);
98        }
99
100        frameWidth = previewWidth + horizontalPadding;
101        frameHeight = previewHeight + verticalPadding;
102
103        int hSpace = ((r - l) - frameWidth) / 2;
104        int vSpace = ((b - t) - frameHeight) / 2;
105        mFrame.measure(
106                MeasureSpec.makeMeasureSpec(frameWidth, MeasureSpec.EXACTLY),
107                MeasureSpec.makeMeasureSpec(frameHeight, MeasureSpec.EXACTLY));
108        mFrame.layout(l + hSpace, t + vSpace, r - hSpace, b - vSpace);
109        if (mSizeListener != null) {
110            mSizeListener.onSizeChanged();
111        }
112    }
113}
114
115