Home | History | Annotate | Download | only in camera
      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.view.ViewStub;
     24 import android.widget.RelativeLayout;
     25 
     26 import com.android.camera.ui.LayoutChangeHelper;
     27 import com.android.camera.ui.LayoutChangeNotifier;
     28 import com.android.gallery3d.common.ApiHelper;
     29 
     30 /**
     31  * A layout which handles the preview aspect ratio.
     32  */
     33 public class PreviewFrameLayout extends RelativeLayout implements LayoutChangeNotifier {
     34 
     35     private static final String TAG = "CAM_preview";
     36 
     37     /** A callback to be invoked when the preview frame's size changes. */
     38     public interface OnSizeChangedListener {
     39         public void onSizeChanged(int width, int height);
     40     }
     41 
     42     private double mAspectRatio;
     43     private View mBorder;
     44     private OnSizeChangedListener mListener;
     45     private LayoutChangeHelper mLayoutChangeHelper;
     46 
     47     public PreviewFrameLayout(Context context, AttributeSet attrs) {
     48         super(context, attrs);
     49         setAspectRatio(4.0 / 3.0);
     50         mLayoutChangeHelper = new LayoutChangeHelper(this);
     51     }
     52 
     53     @Override
     54     protected void onFinishInflate() {
     55         mBorder = findViewById(R.id.preview_border);
     56         if (ApiHelper.HAS_FACE_DETECTION) {
     57             ViewStub faceViewStub = (ViewStub) findViewById(R.id.face_view_stub);
     58             /* preview_frame_video.xml does not have face view stub, so we need to
     59              * check that.
     60              */
     61             if (faceViewStub != null) {
     62                 faceViewStub.inflate();
     63             }
     64         }
     65     }
     66 
     67     public void setAspectRatio(double ratio) {
     68         if (ratio <= 0.0) throw new IllegalArgumentException();
     69 
     70         if (getResources().getConfiguration().orientation
     71                 == Configuration.ORIENTATION_PORTRAIT) {
     72             ratio = 1 / ratio;
     73         }
     74 
     75         if (mAspectRatio != ratio) {
     76             mAspectRatio = ratio;
     77             requestLayout();
     78         }
     79     }
     80 
     81     public void showBorder(boolean enabled) {
     82         mBorder.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
     83     }
     84 
     85     public void fadeOutBorder() {
     86         Util.fadeOut(mBorder);
     87     }
     88 
     89     @Override
     90     protected void onMeasure(int widthSpec, int heightSpec) {
     91         int previewWidth = MeasureSpec.getSize(widthSpec);
     92         int previewHeight = MeasureSpec.getSize(heightSpec);
     93 
     94         // Get the padding of the border background.
     95         int hPadding = getPaddingLeft() + getPaddingRight();
     96         int vPadding = getPaddingTop() + getPaddingBottom();
     97 
     98         // Resize the preview frame with correct aspect ratio.
     99         previewWidth -= hPadding;
    100         previewHeight -= vPadding;
    101 
    102         // Add the padding of the border.
    103         previewWidth += hPadding;
    104         previewHeight += vPadding;
    105 
    106         // Ask children to follow the new preview dimension.
    107         super.onMeasure(MeasureSpec.makeMeasureSpec(previewWidth, MeasureSpec.EXACTLY),
    108                 MeasureSpec.makeMeasureSpec(previewHeight, MeasureSpec.EXACTLY));
    109     }
    110 
    111     public void setOnSizeChangedListener(OnSizeChangedListener listener) {
    112         mListener = listener;
    113     }
    114 
    115     @Override
    116     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    117         if (mListener != null) mListener.onSizeChanged(w, h);
    118     }
    119 
    120     @Override
    121     public void setOnLayoutChangeListener(
    122             LayoutChangeNotifier.Listener listener) {
    123         mLayoutChangeHelper.setOnLayoutChangeListener(listener);
    124     }
    125 
    126     @Override
    127     protected void onLayout(boolean changed, int l, int t, int r, int b) {
    128         super.onLayout(changed, l, t, r, b);
    129         mLayoutChangeHelper.onLayout(changed, l, t, r, b);
    130     }
    131 }
    132