Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 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 | |
| 17 | package com.android.camera; |
| 18 | |
Cheng-Ru Lin | 2bc4714 | 2009-09-30 11:57:50 +0800 | [diff] [blame] | 19 | import android.app.Activity; |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.util.AttributeSet; |
Cheng-Ru Lin | 2bc4714 | 2009-09-30 11:57:50 +0800 | [diff] [blame] | 22 | import android.util.DisplayMetrics; |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 23 | import android.view.View; |
| 24 | import android.view.ViewGroup; |
| 25 | import android.widget.ImageView; |
| 26 | import android.widget.FrameLayout; |
| 27 | |
| 28 | public class PreviewFrameLayout extends ViewGroup { |
Cheng-Ru Lin | 2bc4714 | 2009-09-30 11:57:50 +0800 | [diff] [blame] | 29 | private static final int MIN_HORIZONTAL_MARGIN = 10; // 10dp |
| 30 | |
Wei-Ta Chen | 1f0564e | 2009-09-28 18:38:05 -0700 | [diff] [blame] | 31 | public interface OnSizeChangedListener { |
| 32 | public void onSizeChanged(); |
| 33 | } |
| 34 | |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 35 | private double mAspectRatio = 4.0 / 3.0; |
| 36 | private ImageView mGripper; |
| 37 | private FrameLayout mFrame; |
Wei-Ta Chen | 1f0564e | 2009-09-28 18:38:05 -0700 | [diff] [blame] | 38 | private OnSizeChangedListener mSizeListener; |
Cheng-Ru Lin | 2bc4714 | 2009-09-30 11:57:50 +0800 | [diff] [blame] | 39 | private DisplayMetrics mMetrics = new DisplayMetrics(); |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 40 | |
| 41 | public PreviewFrameLayout(Context context, AttributeSet attrs) { |
| 42 | super(context, attrs); |
Cheng-Ru Lin | 2bc4714 | 2009-09-30 11:57:50 +0800 | [diff] [blame] | 43 | ((Activity) context).getWindowManager() |
| 44 | .getDefaultDisplay().getMetrics(mMetrics); |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 45 | } |
| 46 | |
Wei-Ta Chen | 1f0564e | 2009-09-28 18:38:05 -0700 | [diff] [blame] | 47 | public void setOnSizeChangedListener(OnSizeChangedListener listener) { |
| 48 | mSizeListener = listener; |
| 49 | } |
| 50 | |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 51 | @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 Lin | 2bc4714 | 2009-09-30 11:57:50 +0800 | [diff] [blame] | 83 | int frameWidth = getMeasuredWidth() - (int) Math.max( |
| 84 | gripperWidth, MIN_HORIZONTAL_MARGIN * mMetrics.density); |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 85 | 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 Chen | 1f0564e | 2009-09-28 18:38:05 -0700 | [diff] [blame] | 134 | if (mSizeListener != null) { |
| 135 | mSizeListener.onSizeChanged(); |
| 136 | } |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 137 | } |
| 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 | |