2016-06-08 11 views
1

카메라 조각에 전체 화면으로 텍스처보기를 설정하고 싶습니다. 바로 아래 부분이 흰색 블록으로 나타납니다.안드로이드 전체 화면 카메라

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

대신 wrap_content의 match_parent하기 위해 latyout_height와 layout_width를 설정합니다.

다른 버튼을 사용하지 않는 한, 버튼이있는 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

차이가 없습니다. 나는 아직도 공백을 볼 수있다. –

관련 문제