PreviewFrameLayout.java revision e480f77352d670b2892a9b7b7cd503838b71ca9f
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;
25import android.widget.ImageView;
26import android.widget.FrameLayout;
27
28import com.android.camera.R;
29
30/**
31 * A layout which handles the preview aspect ratio and the position of
32 * the gripper.
33 */
34public class PreviewFrameLayout extends ViewGroup {
35    private static final int MIN_HORIZONTAL_MARGIN = 10; // 10dp
36
37    /** A callback to be invoked when the preview frame's size changes. */
38    public interface OnSizeChangedListener {
39        public void onSizeChanged();
40    }
41
42    private double mAspectRatio = 4.0 / 3.0;
43    private ImageView mGripper;
44    private FrameLayout mFrame;
45    private OnSizeChangedListener mSizeListener;
46    private DisplayMetrics mMetrics = new DisplayMetrics();
47
48    public PreviewFrameLayout(Context context, AttributeSet attrs) {
49        super(context, attrs);
50        ((Activity) context).getWindowManager()
51                .getDefaultDisplay().getMetrics(mMetrics);
52    }
53
54    public void setOnSizeChangedListener(OnSizeChangedListener listener) {
55        mSizeListener = listener;
56    }
57
58    @Override
59    protected void onFinishInflate() {
60        mGripper = (ImageView) findViewById(R.id.btn_gripper);
61        mFrame = (FrameLayout) findViewById(R.id.frame);
62        if (mFrame == null) {
63            throw new IllegalStateException(
64                    "must provide child with id as \"frame\"");
65        }
66    }
67
68    public void setAspectRatio(double ratio) {
69        if (ratio <= 0.0) throw new IllegalArgumentException();
70
71        if (mAspectRatio != ratio) {
72            mAspectRatio = ratio;
73            requestLayout();
74        }
75    }
76
77    @Override
78    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
79        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
80
81        int gripperWidth = 0;
82        int gripperHeight = 0;
83
84        if (mGripper != null) {
85            measureChild(mGripper, widthMeasureSpec, heightMeasureSpec);
86            gripperWidth = mGripper.getMeasuredWidth();
87            gripperHeight = mGripper.getMeasuredHeight();
88        }
89
90        int frameWidth = getMeasuredWidth() - (int) Math.max(
91                gripperWidth, MIN_HORIZONTAL_MARGIN * mMetrics.density);
92        int frameHeight = getMeasuredHeight();
93
94        FrameLayout f = mFrame;
95
96        int horizontalPadding = f.getPaddingLeft() + f.getPaddingRight();
97        int verticalPadding = f.getPaddingBottom() + f.getPaddingTop();
98
99        int previewWidth = frameWidth - horizontalPadding;
100        int previewHeight = frameHeight - verticalPadding;
101
102        // resize frame and preview for aspect ratio
103        if (previewWidth > previewHeight * mAspectRatio) {
104            previewWidth = (int) (previewHeight * mAspectRatio + .5);
105        } else {
106            previewHeight = (int) (previewWidth / mAspectRatio + .5);
107        }
108        frameWidth = previewWidth + horizontalPadding;
109        frameHeight = previewHeight + verticalPadding;
110
111        measureChild(mFrame,
112                MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY, frameWidth),
113                MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY, frameHeight));
114    }
115
116    @Override
117    protected void onLayout(boolean changed, int l, int t, int r, int b) {
118        // Try to layout the "frame" in the center of the area, and put
119        // "gripper" just to the left of it. If there is no enough space for
120        // the gripper, the "frame" will be moved a little right so that
121        // they won't overlap with each other.
122
123        int frameWidth = mFrame.getMeasuredWidth();
124        int frameHeight = mFrame.getMeasuredHeight();
125
126        int leftSpace = ((r - l) - frameWidth) / 2;
127        int topSpace = ((b - t) - frameHeight) / 2;
128
129        int gripperWidth = 0;
130        int gripperHeight = 0;
131        if (mGripper != null) {
132            gripperWidth = mGripper.getMeasuredWidth();
133            gripperHeight = mGripper.getMeasuredHeight();
134            myLayoutChild(mGripper,
135                    Math.max(l, l + (leftSpace - gripperWidth)),
136                    t + ((b - t) - gripperHeight) / 2,
137                    gripperWidth, gripperHeight);
138        }
139        myLayoutChild(mFrame, Math.max(l + leftSpace, l + gripperWidth),
140                t + topSpace, frameWidth, frameHeight);
141        if (mSizeListener != null) {
142            mSizeListener.onSizeChanged();
143        }
144    }
145
146    private static void myLayoutChild(View child, int l, int t, int w, int h) {
147        child.layout(l, t, l + w, t + h);
148    }
149}
150
151