Skip to content

Most visited

Recently visited

navigation
Camera2Video / src / com.example.android.camera2video /

AutoFitTextureView.java

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.android.camera2video;
18
 
19
import android.content.Context;
20
import android.util.AttributeSet;
21
import android.view.TextureView;
22
 
23
/**
24
 * A {@link TextureView} that can be adjusted to a specified aspect ratio.
25
 */
26
public class AutoFitTextureView extends TextureView {
27
 
28
    private int mRatioWidth = 0;
29
    private int mRatioHeight = 0;
30
 
31
    public AutoFitTextureView(Context context) {
32
        this(context, null);
33
    }
34
 
35
    public AutoFitTextureView(Context context, AttributeSet attrs) {
36
        this(context, attrs, 0);
37
    }
38
 
39
    public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
40
        super(context, attrs, defStyle);
41
    }
42
 
43
    /**
44
     * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
45
     * calculated from the parameters. Note that the actual sizes of parameters don't matter, that
46
     * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
47
     *
48
     * @param width  Relative horizontal size
49
     * @param height Relative vertical size
50
     */
51
    public void setAspectRatio(int width, int height) {
52
        if (width < 0 || height < 0) {
53
            throw new IllegalArgumentException("Size cannot be negative.");
54
        }
55
        mRatioWidth = width;
56
        mRatioHeight = height;
57
        requestLayout();
58
    }
59
 
60
    @Override
61
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
62
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
63
        int width = MeasureSpec.getSize(widthMeasureSpec);
64
        int height = MeasureSpec.getSize(heightMeasureSpec);
65
        if (0 == mRatioWidth || 0 == mRatioHeight) {
66
            setMeasuredDimension(width, height);
67
        } else {
68
            if (width < height * mRatioWidth / mRatioHeight) {
69
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
70
            } else {
71
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
72
            }
73
        }
74
    }
75
 
76
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)