(AutoFitTextureView) findViewById(R.id.texture);//nullになります。
何故なのでしょうか?
xmlファイルでは、このように指定しています。
xml
1<com.google.android.exoplayer2.demo.AutoFitTextureView 2 android:id="@+id/texture" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 android:layout_alignParentBottom="true" 6 android:layout_alignParentStart="true" 7 android:layout_alignParentTop="true" /> 8
AutoFitTextureViewのjavaクラスです。
java
1package com.google.android.exoplayer2.demo; 2 3import android.content.Context; 4import android.util.AttributeSet; 5import android.view.TextureView; 6 7import android.content.Context; 8import android.util.AttributeSet; 9import android.view.TextureView; 10 11public class AutoFitTextureView extends TextureView { 12 13 private int mRatioWidth = 0; 14 private int mRatioHeight = 0; 15 16 public AutoFitTextureView(Context context) { 17 this(context, null); 18 } 19 20 public AutoFitTextureView(Context context, AttributeSet attrs) { 21 this(context, attrs, 0); 22 } 23 24 public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) { 25 super(context, attrs, defStyle); 26 } 27 28 /** 29 * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio 30 * calculated from the parameters. Note that the actual sizes of parameters don't matter, that 31 * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result. 32 * 33 * @param width Relative horizontal size 34 * @param height Relative vertical size 35 */ 36 public void setAspectRatio(int width, int height) { 37 if (width < 0 || height < 0) { 38 throw new IllegalArgumentException("Size cannot be negative."); 39 } 40 mRatioWidth = width; 41 mRatioHeight = height; 42 requestLayout(); 43 } 44 45 @Override 46 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 47 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 48 int width = MeasureSpec.getSize(widthMeasureSpec); 49 int height = MeasureSpec.getSize(heightMeasureSpec); 50 if (0 == mRatioWidth || 0 == mRatioHeight) { 51 setMeasuredDimension(width, height); 52 } else { 53 if (width < height * mRatioWidth / mRatioHeight) { 54 setMeasuredDimension(width, width * mRatioHeight / mRatioWidth); 55 } else { 56 setMeasuredDimension(height * mRatioWidth / mRatioHeight, height); 57 } 58 } 59 } 60 61} 62 63
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/09 01:20