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