This source file includes following definitions.
- setAspectRatio
- onMeasure
package com.example.helloandroidcamera2;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;
public class AutoFitSurfaceView extends SurfaceView {
    private static final String TAG = "AutoFitSurfaceView";
    private int mRatioWidth = 0;
    private int mRatioHeight = 0;
    public AutoFitSurfaceView(Context context) {
        this(context, null);
    }
    public AutoFitSurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public AutoFitSurfaceView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    public void setAspectRatio(int width, int height) {
        if (width < 0 || height < 0) {
            throw new IllegalArgumentException("Size cannot be negative.");
        }
        mRatioWidth = width;
        mRatioHeight = height;
        requestLayout();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (0 == mRatioWidth || 0 == mRatioHeight) {
            Log.d(TAG, String.format("aspect ratio is 0 x 0 (uninitialized), setting measured"
                    + " dimension to: %d x %d", width, height));
            setMeasuredDimension(width, height);
        } else {
            if (width < height * mRatioWidth / mRatioHeight) {
                Log.d(TAG, String.format("setting measured dimension to %d x %d", width, height));
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
            } else {
                Log.d(TAG, String.format("setting measured dimension to %d x %d", width, height));
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
            }
        }
    }