From d92108bb8d8548f0760fb103286751c62497459d Mon Sep 17 00:00:00 2001 From: Jay Wang Date: Tue, 15 Mar 2016 14:15:43 -0700 Subject: SnapdragonCamera: Add logic to calculate preview surface ratio new file is copied from the commit : 51c65e100d6221c8e19d1345e40db2e7d1c5a190 src/com/android/camera/ui/AutoFitSurfaceView.java copied from media/Camera2Basic/Application/src/main/java/com/example/android/ camera2basic/AutoFitTextureView.java CRs-Fixed: 989750 Change-Id: I29a6fd2219991bda9aaa64f174a61b0aa5b71e87 --- src/com/android/camera/ui/AutoFitSurfaceView.java | 76 +++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/com/android/camera/ui/AutoFitSurfaceView.java diff --git a/src/com/android/camera/ui/AutoFitSurfaceView.java b/src/com/android/camera/ui/AutoFitSurfaceView.java new file mode 100644 index 000000000..f4903e19f --- /dev/null +++ b/src/com/android/camera/ui/AutoFitSurfaceView.java @@ -0,0 +1,76 @@ +/* + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.camera2basic; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.TextureView; + +/** + * A {@link TextureView} that can be adjusted to a specified aspect ratio. + */ +public class AutoFitTextureView extends TextureView { + + private int mRatioWidth = 0; + private int mRatioHeight = 0; + + public AutoFitTextureView(Context context) { + this(context, null); + } + + public AutoFitTextureView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + /** + * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio + * calculated from the parameters. Note that the actual sizes of parameters don't matter, that + * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result. + * + * @param width Relative horizontal size + * @param height Relative vertical size + */ + 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) { + setMeasuredDimension(width, height); + } else { + if (width < height * mRatioWidth / mRatioHeight) { + setMeasuredDimension(width, width * mRatioHeight / mRatioWidth); + } else { + setMeasuredDimension(height * mRatioWidth / mRatioHeight, height); + } + } + } + +} -- cgit v1.2.3