1 /*
2  * Copyright 2014 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.example.helloandroidcamera2;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.util.Log;
22 import android.view.SurfaceView;
23 
24 /**
25  * A {@link SurfaceView} that can be adjusted to a specified aspect ratio.
26  */
27 public class AutoFitSurfaceView extends SurfaceView {
28 
29     private static final String TAG = "AutoFitSurfaceView";
30 
31     private int mRatioWidth = 0;
32     private int mRatioHeight = 0;
33 
AutoFitSurfaceView(Context context)34     public AutoFitSurfaceView(Context context) {
35         this(context, null);
36     }
37 
AutoFitSurfaceView(Context context, AttributeSet attrs)38     public AutoFitSurfaceView(Context context, AttributeSet attrs) {
39         this(context, attrs, 0);
40     }
41 
AutoFitSurfaceView(Context context, AttributeSet attrs, int defStyle)42     public AutoFitSurfaceView(Context context, AttributeSet attrs, int defStyle) {
43         super(context, attrs, defStyle);
44     }
45 
46     /**
47      * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
48      * calculated from the parameters. Note that the actual sizes of parameters don't matter, that
49      * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
50      *
51      * @param width  Relative horizontal size
52      * @param height Relative vertical size
53      */
setAspectRatio(int width, int height)54     public void setAspectRatio(int width, int height) {
55         if (width < 0 || height < 0) {
56             throw new IllegalArgumentException("Size cannot be negative.");
57         }
58         mRatioWidth = width;
59         mRatioHeight = height;
60         requestLayout();
61     }
62 
63     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)64     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
65         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
66         int width = MeasureSpec.getSize(widthMeasureSpec);
67         int height = MeasureSpec.getSize(heightMeasureSpec);
68         if (0 == mRatioWidth || 0 == mRatioHeight) {
69             Log.d(TAG, String.format("aspect ratio is 0 x 0 (uninitialized), setting measured"
70                     + " dimension to: %d x %d", width, height));
71             setMeasuredDimension(width, height);
72         } else {
73             if (width < height * mRatioWidth / mRatioHeight) {
74                 Log.d(TAG, String.format("setting measured dimension to %d x %d", width, height));
75                 setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
76             } else {
77                 Log.d(TAG, String.format("setting measured dimension to %d x %d", width, height));
78                 setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
79             }
80         }
81     }
82 
83 }