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 19 import android.content.Context; 20 import android.content.res.Configuration; 21 import android.util.AttributeSet; 22 import android.view.View; 23 import android.widget.RelativeLayout; 24 25 /** 26 * A layout which handles the preview aspect ratio. 27 */ 28 public class PreviewFrameLayout extends RelativeLayout { 29 /** A callback to be invoked when the preview frame's size changes. */ 30 public interface OnSizeChangedListener { 31 public void onSizeChanged(int width, int height); 32 } 33 34 private double mAspectRatio; 35 private View mBorder; 36 private OnSizeChangedListener mListener; 37 38 public PreviewFrameLayout(Context context, AttributeSet attrs) { 39 super(context, attrs); 40 setAspectRatio(4.0 / 3.0); 41 } 42 43 @Override 44 protected void onFinishInflate() { 45 mBorder = findViewById(R.id.preview_border); 46 } 47 48 public void setAspectRatio(double ratio) { 49 if (ratio <= 0.0) throw new IllegalArgumentException(); 50 51 if (getResources().getConfiguration().orientation 52 == Configuration.ORIENTATION_PORTRAIT) { 53 ratio = 1 / ratio; 54 } 55 56 if (mAspectRatio != ratio) { 57 mAspectRatio = ratio; 58 requestLayout(); 59 } 60 } 61 62 public void showBorder(boolean enabled) { 63 mBorder.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE); 64 } 65 66 public void fadeOutBorder() { 67 Util.fadeOut(mBorder); 68 } 69 70 @Override 71 protected void onMeasure(int widthSpec, int heightSpec) { 72 int previewWidth = MeasureSpec.getSize(widthSpec); 73 int previewHeight = MeasureSpec.getSize(heightSpec); 74 75 // Get the padding of the border background. 76 int hPadding = getPaddingLeft() + getPaddingRight(); 77 int vPadding = getPaddingTop() + getPaddingBottom(); 78 79 // Resize the preview frame with correct aspect ratio. 80 previewWidth -= hPadding; 81 previewHeight -= vPadding; 82 if (previewWidth > previewHeight * mAspectRatio) { 83 previewWidth = (int) (previewHeight * mAspectRatio + .5); 84 } else { 85 previewHeight = (int) (previewWidth / mAspectRatio + .5); 86 } 87 88 // Add the padding of the border. 89 previewWidth += hPadding; 90 previewHeight += vPadding; 91 92 // Ask children to follow the new preview dimension. 93 super.onMeasure(MeasureSpec.makeMeasureSpec(previewWidth, MeasureSpec.EXACTLY), 94 MeasureSpec.makeMeasureSpec(previewHeight, MeasureSpec.EXACTLY)); 95 } 96 97 public void setOnSizeChangedListener(OnSizeChangedListener listener) { 98 mListener = listener; 99 } 100 101 @Override 102 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 103 if (mListener != null) mListener.onSizeChanged(w, h); 104 } 105 } 106