diff options
| author | Trevor Johns <trevorjohns@google.com> | 2014-10-17 03:31:00 -0700 |
|---|---|---|
| committer | Trevor Johns <trevorjohns@google.com> | 2014-10-17 03:31:00 -0700 |
| commit | 6876a9cfc5eb05168ae7fa5215979f22396fff58 (patch) | |
| tree | b991f731cfe7256f3cef6a9e55d6b3174cf2e05f /samples/browseable/Camera2Basic/src/com.example.android.camera2basic/AutoFitTextureView.java | |
| parent | 66b6f721b7e20911f0764720346caca5c77fb22b (diff) | |
Add browseable samples for L SDK release
Change-Id: I71c6ff9a90b7734042d68af7f01e6d61118cc508
Diffstat (limited to 'samples/browseable/Camera2Basic/src/com.example.android.camera2basic/AutoFitTextureView.java')
| -rw-r--r-- | samples/browseable/Camera2Basic/src/com.example.android.camera2basic/AutoFitTextureView.java | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/samples/browseable/Camera2Basic/src/com.example.android.camera2basic/AutoFitTextureView.java b/samples/browseable/Camera2Basic/src/com.example.android.camera2basic/AutoFitTextureView.java new file mode 100644 index 000000000..f4903e19f --- /dev/null +++ b/samples/browseable/Camera2Basic/src/com.example.android.camera2basic/AutoFitTextureView.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); + } + } + } + +} |
