Home | History | Annotate | Download | only in tflitecamerademo
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 package com.example.android.tflitecamerademo;
     17 
     18 import android.content.Context;
     19 import android.util.AttributeSet;
     20 import android.view.TextureView;
     21 
     22 /** A {@link TextureView} that can be adjusted to a specified aspect ratio. */
     23 public class AutoFitTextureView extends TextureView {
     24 
     25   private int mRatioWidth = 0;
     26   private int mRatioHeight = 0;
     27 
     28   public AutoFitTextureView(Context context) {
     29     this(context, null);
     30   }
     31 
     32   public AutoFitTextureView(Context context, AttributeSet attrs) {
     33     this(context, attrs, 0);
     34   }
     35 
     36   public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
     37     super(context, attrs, defStyle);
     38   }
     39 
     40   /**
     41    * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
     42    * calculated from the parameters. Note that the actual sizes of parameters don't matter, that is,
     43    * calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
     44    *
     45    * @param width Relative horizontal size
     46    * @param height Relative vertical size
     47    */
     48   public void setAspectRatio(int width, int height) {
     49     if (width < 0 || height < 0) {
     50       throw new IllegalArgumentException("Size cannot be negative.");
     51     }
     52     mRatioWidth = width;
     53     mRatioHeight = height;
     54     requestLayout();
     55   }
     56 
     57   @Override
     58   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     59     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     60     int width = MeasureSpec.getSize(widthMeasureSpec);
     61     int height = MeasureSpec.getSize(heightMeasureSpec);
     62     if (0 == mRatioWidth || 0 == mRatioHeight) {
     63       setMeasuredDimension(width, height);
     64     } else {
     65       if (width < height * mRatioWidth / mRatioHeight) {
     66         setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
     67       } else {
     68         setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
     69       }
     70     }
     71   }
     72 }
     73