2016-06-17 13 views
1

Ich habe ein Problem mit meiner App, wo die Kamera nicht auf das Ende des Bildschirms auf der rechten Seite passt und hat einen riesigen schwarzen Balken. Bitte helfen Sie. Der Code für die Steuerung der Kamera ist nicht enthalten, da zu viel ausgegeben wird, um den Stack-Überlauf auszulösen. Die Größe der Kamera liegt hauptsächlich in den Layouts und der AutoFitTextureView.java. Bitte helfen Sie.Kamera in meiner App füllt nicht den Bildschirm

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

Haben Sie versucht, android: layout_width = "match_parent" anstelle von 500dp? –

+0

versuchen, von LinearLayout anstelle von RelativeLayout als Root-Ansicht und verwenden von Android: layout_weightSum zum Messen der beiden Kind-Layout –

Antwort

0

Versuchen Sie, es statt 500dp match_parent:

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

Es hat nicht funktioniert, denke ich, dass die AutoFitTexture-Klasse die falschen Dimensionen für mein Telefon bekommt. –

+0

Ich habe den Code für die Kamera geändert –

+0

hi @NoahTanenholtz - wie hast du das geändert ?! Vielen Dank... – Fattie