2016-06-08 112 views
1

我想将纹理视图设置为全屏相机片段。现在底部显示为白色块。android全屏相机

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); 
      } 
     } 
    } 

} 

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.example.AutoFitTextureView 
     android:id="@+id/texture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" /> 

    <FrameLayout 
     android:id="@+id/control" 
     android:layout_width="match_parent" 
     android:layout_height="80dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:background="@android:color/transparent"> 
     <Button 
      android:id="@+id/picture" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="@string/picture" /> 
    </FrameLayout> 

</RelativeLayout> 

回答

0

设置你的latyout_height和layout_width到match_parent而不是WRAP_CONTENT。

我也摆脱了你的按钮所在的FrameLayout,只需将你的按钮放在屏幕的底部,除非你用它来做其他事情。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.example.AutoFitTextureView 
     android:id="@+id/texture" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" /> 

    <Button 
     android:id="@+id/picture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="@string/picture"/> 

</RelativeLayout> 
+0

不过,它不工作。现在'图片'按钮位于左下角。 –

+0

@RonakPatel应该现在就工作。改为将其编辑为fill_parent。 – Jay

+0

没有区别。我仍然可以看到白色空间。 –