blob: 7ef92068cc017efc73f76e113069ce45cdbc33fc [file] [log] [blame]
Cheng-Ru Linffcca742009-09-28 03:21:25 +08001/*
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
Cheng-Ru Lin2bc47142009-09-30 11:57:50 +080019import android.app.Activity;
Cheng-Ru Linffcca742009-09-28 03:21:25 +080020import android.content.Context;
21import android.util.AttributeSet;
Cheng-Ru Lin2bc47142009-09-30 11:57:50 +080022import android.util.DisplayMetrics;
Cheng-Ru Linffcca742009-09-28 03:21:25 +080023import android.view.View;
24import android.view.ViewGroup;
25import android.widget.ImageView;
26import android.widget.FrameLayout;
27
28public class PreviewFrameLayout extends ViewGroup {
Cheng-Ru Lin2bc47142009-09-30 11:57:50 +080029 private static final int MIN_HORIZONTAL_MARGIN = 10; // 10dp
30
Wei-Ta Chen1f0564e2009-09-28 18:38:05 -070031 public interface OnSizeChangedListener {
32 public void onSizeChanged();
33 }
34
Cheng-Ru Linffcca742009-09-28 03:21:25 +080035 private double mAspectRatio = 4.0 / 3.0;
36 private ImageView mGripper;
37 private FrameLayout mFrame;
Wei-Ta Chen1f0564e2009-09-28 18:38:05 -070038 private OnSizeChangedListener mSizeListener;
Cheng-Ru Lin2bc47142009-09-30 11:57:50 +080039 private DisplayMetrics mMetrics = new DisplayMetrics();
Cheng-Ru Linffcca742009-09-28 03:21:25 +080040
41 public PreviewFrameLayout(Context context, AttributeSet attrs) {
42 super(context, attrs);
Cheng-Ru Lin2bc47142009-09-30 11:57:50 +080043 ((Activity) context).getWindowManager()
44 .getDefaultDisplay().getMetrics(mMetrics);
Cheng-Ru Linffcca742009-09-28 03:21:25 +080045 }
46
Wei-Ta Chen1f0564e2009-09-28 18:38:05 -070047 public void setOnSizeChangedListener(OnSizeChangedListener listener) {
48 mSizeListener = listener;
49 }
50
Cheng-Ru Linffcca742009-09-28 03:21:25 +080051 @Override
52 protected void onFinishInflate() {
53 mGripper = (ImageView) findViewById(R.id.btn_gripper);
54 mFrame = (FrameLayout) 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 onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
72 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
73
74 int gripperWidth = 0;
75 int gripperHeight = 0;
76
77 if (mGripper != null) {
78 measureChild(mGripper, widthMeasureSpec, heightMeasureSpec);
79 gripperWidth = mGripper.getMeasuredWidth();
80 gripperHeight = mGripper.getMeasuredHeight();
81 }
82
Cheng-Ru Lin2bc47142009-09-30 11:57:50 +080083 int frameWidth = getMeasuredWidth() - (int) Math.max(
84 gripperWidth, MIN_HORIZONTAL_MARGIN * mMetrics.density);
Cheng-Ru Linffcca742009-09-28 03:21:25 +080085 int frameHeight = getMeasuredHeight();
86
87 FrameLayout f = mFrame;
88
89 int horizontalPadding = f.getPaddingLeft() + f.getPaddingRight();
90 int verticalPadding = f.getPaddingBottom() + f.getPaddingTop();
91
92 int previewWidth = frameWidth - horizontalPadding;
93 int previewHeight = frameHeight - verticalPadding;
94
95 // resize frame and preview for aspect ratio
96 if (previewWidth > previewHeight * mAspectRatio) {
97 previewWidth = (int) (previewHeight * mAspectRatio + .5);
98 } else {
99 previewHeight = (int) (previewWidth / mAspectRatio + .5);
100 }
101 frameWidth = previewWidth + horizontalPadding;
102 frameHeight = previewHeight + verticalPadding;
103
104 measureChild(mFrame,
105 MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY, frameWidth),
106 MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY, frameHeight));
107 }
108
109 @Override
110 protected void onLayout(boolean changed, int l, int t, int r, int b) {
111 // Try to layout the "frame" in the center of the area, and put
112 // "gripper" just to the left of it. If there is no enough space for
113 // the gripper, the "frame" will be moved a little right so that
114 // they won't overlap with each other.
115
116 int frameWidth = mFrame.getMeasuredWidth();
117 int frameHeight = mFrame.getMeasuredHeight();
118
119 int leftSpace = ((r - l) - frameWidth) / 2;
120 int topSpace = ((b - t) - frameHeight) / 2;
121
122 int gripperWidth = 0;
123 int gripperHeight = 0;
124 if (mGripper != null) {
125 gripperWidth = mGripper.getMeasuredWidth();
126 gripperHeight = mGripper.getMeasuredHeight();
127 myLayoutChild(mGripper,
128 Math.max(l, l + (leftSpace - gripperWidth)),
129 t + ((b - t) - gripperHeight) / 2,
130 gripperWidth, gripperHeight);
131 }
132 myLayoutChild(mFrame, Math.max(l + leftSpace, l + gripperWidth),
133 t + topSpace, frameWidth, frameHeight);
Wei-Ta Chen1f0564e2009-09-28 18:38:05 -0700134 if (mSizeListener != null) {
135 mSizeListener.onSizeChanged();
136 }
Cheng-Ru Linffcca742009-09-28 03:21:25 +0800137 }
138
139 private static void myLayoutChild(View child, int l, int t, int w, int h) {
140 child.layout(l, t, l + w, t + h);
141 }
142}
143