I have a problem with my app where the camera does not fit to the end of the screen on the right side and has a giant black bar. Please help. The code for controlling the camera is not included because there is too much output to put on stack overflow. The size of the camera is mainly in the layouts and the AutoFitTextureView.java. Please help.
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;
}
}
Try setting it to match_parent instead of 500dp:
android:layout_width="match_parent"
android:layout_height="match_parent"