我的应用程序有问题,相机不适合右侧屏幕的末端,并有一个巨大的黑色条。请帮忙。不包括用于控制摄像机的代码,因为存在太多输出以使堆栈溢出。摄像头的大小主要在布局和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 :(得分:0)
尝试将其设置为match_parent而不是500dp:
android:layout_width="match_parent"
android:layout_height="match_parent"