2016-06-08 2 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

Установите 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

Все еще, это не работает. Теперь кнопка 'picture' находится в левом нижнем углу. –

+0

@RonakPatel Должно работать сейчас. Отредактировал его вместо fill_parent. – Jay

+0

Нет разницы. Я все еще вижу пустое пространство. –