2016-06-17 4 views
1

У меня проблема с моим приложением, где камера не подходит к концу экрана с правой стороны и имеет гигантскую черную полосу. Пожалуйста помоги. Код для управления камерой не включен, потому что слишком много выходных данных для переполнения стека. Размер камеры в основном состоит из макетов и AutoFitTextureView.java. Пожалуйста помоги.Камера в моем приложении не заполняет экран

camera2_basic.xml

<?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.technologyfortomorrow.animalyze.AutoFitTextureView 
     android:id="@+id/texture" 
     android:layout_width="500dp" 
     android:layout_height="580dp" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" /> 


    <Button 
     android:id="@+id/picture" 
     android:layout_width="75dp" 
     android:layout_height="75dp" 
     android:layout_gravity="center" 
     android:background="@drawable/circle" 
     android:layout_marginBottom="18dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

AutoFitTextureView.java

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


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

} 

MainActivity.java

public class MainActivity extends FragmentActivity { 
    private CameraCaptureSession mSession; 
    private CaptureRequest.Builder mBuilder; 
    private CameraDevice mCameraDevice; 
    private CameraManager mCameraManager; 
    static ViewPager pager; 
    Boolean isOn = false; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if (null == savedInstanceState) { 
      getFragmentManager().beginTransaction() 
        .replace(R.id.container, Camera2BasicFragment.newInstance()) 
        .commit(); 
     } 



     final Button bearPaw = (Button) findViewById(R.id.bearPaw); 
     bearPaw.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(MainActivity.this, NewsFeed.class); 
       startActivity(intent); 
       overridePendingTransition(R.anim.right_in, R.anim.right_out); 

      } 
     }); 

     final Button flashOn = (Button) findViewById(R.id.flah_off); 
     flashOn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (isOn) { 
        flashOn.setBackgroundResource(R.drawable.flash_on); 
        turnOnFlashLight(); 
       } else { 
        flashOn.setBackgroundResource(R.drawable.flah_off); 
        turnOffFlashLight(); 
       } 
       isOn = !isOn; 
      } 
     }); 
    } 



// Since this is an object collection, use a FragmentStatePagerAdapter, 
// and NOT a FragmentPagerAdapter. 


    public void turnOnFlashLight() { 
     try { 
      mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH); 
      mSession.setRepeatingRequest(mBuilder.build(), null, null); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void turnOffFlashLight() { 
     try { 
      mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF); 
      mSession.setRepeatingRequest(mBuilder.build(), null, null); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void close() { 
     if (mCameraDevice == null || mSession == null) { 
      return; 
     } 
     mSession.close(); 
     mCameraDevice.close(); 
     mCameraDevice = null; 
     mSession = null; 
    } 





    } 
+0

Вы пробовали использовать Android: layout_width = "match_parent" вместо 500dp? –

+0

попробуйте использовать LinearLayout вместо RelativeLayout в качестве своего корневого представления и использовать с android: layout_weightSum для измерения двух дочерних макетов –

ответ

0

Попробуйте установить его match_parent вместо 500dp:

android:layout_width="match_parent" 
android:layout_height="match_parent" 
+0

Это не сработало, я думаю, что класс AutoFitTexture получает неправильные размеры для моего телефона. –

+0

Я изменил код для камеры –

+0

привет @NoahTanenholtz - каким образом вы его изменили?! благодаря... – Fattie

Смежные вопросы