android - LinearLayout 和 CameraSource 不填满屏幕
问题描述
我正在尝试在我的应用程序中构建一个全屏条形码扫描仪,但我无法获得LinearLayout
仅包含我CameraSource
以填充整个屏幕的扫描仪(这篇文章的底部是我在模拟器中使用模拟相机的结果的屏幕截图)。
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/topLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mattdonders.android.wppcalculator.barcode.CameraSourcePreview
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
这是我的 BarcodeScanner 活动中的 onCreate 方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode_scanner);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
mPreview = (CameraSourcePreview) findViewById(R.id.camera_preview);
// Check for the camera permission before accessing the camera. If the
// permission is not granted yet, request permission.
int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if (rc == PackageManager.PERMISSION_GRANTED) {
createCameraSource();
} else {
requestCameraPermission();
}
}
下面是 CameraSource 类的构造函数:
public CameraSourcePreview(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mStartRequested = false;
mSurfaceAvailable = false;
mSurfaceView = new SurfaceView(context);
mSurfaceView.getHolder().addCallback(new SurfaceCallback());
addView(mSurfaceView);
}
解决方案
正如我在原帖的评论中提到的,这个问题的答案在于onLayout
CameraSourcePreview 类的方法。基于实施 Github 问题中提到的一些解决方案,这个答案对我来说效果最好,我已经复制/粘贴了下面的代码,所以这个问题的答案不仅仅是一个链接。
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int previewWidth = 320;
int previewHeight = 240;
if (mCameraSource != null) {
Size size = mCameraSource.getPreviewSize();
if (size != null) {
previewWidth = size.getWidth();
previewHeight = size.getHeight();
}
}
// Swap width and height sizes when in portrait, since it will be rotated 90 degrees
if (isPortraitMode()) {
int tmp = previewWidth;
previewWidth = previewHeight;
previewHeight = tmp;
}
final int viewWidth = right - left;
final int viewHeight = bottom - top;
int childWidth;
int childHeight;
int childXOffset = 0;
int childYOffset = 0;
float widthRatio = (float) viewWidth / (float) previewWidth;
float heightRatio = (float) viewHeight / (float) previewHeight;
// To fill the view with the camera preview, while also preserving the correct aspect ratio,
// it is usually necessary to slightly oversize the child and to crop off portions along one
// of the dimensions. We scale up based on the dimension requiring the most correction, and
// compute a crop offset for the other dimension.
if (widthRatio > heightRatio) {
childWidth = viewWidth;
childHeight = (int) ((float) previewHeight * widthRatio);
childYOffset = (childHeight - viewHeight) / 2;
} else {
childWidth = (int) ((float) previewWidth * heightRatio);
childHeight = viewHeight;
childXOffset = (childWidth - viewWidth) / 2;
}
for (int i = 0; i < getChildCount(); ++i) {
// One dimension will be cropped. We shift child over or up by this offset and adjust
// the size to maintain the proper aspect ratio.
getChildAt(i).layout(
-1 * childXOffset, -1 * childYOffset,
childWidth - childXOffset, childHeight - childYOffset);
}
try {
startIfReady();
} catch (IOException e) {
Log.e(TAG, "Could not start camera source.", e);
}
}
推荐阅读
- javascript - Node js 对接会 Socket io 的两个概念我说的对吗?
- php - 是否可以为“IntlDateFormatter::FULL”IntlDateFormatter 日期/时间格式关闭“秒”和“最后的长文本”?
- python - 烧瓶分页的问题
- r - 在 R 中使用 rgl 包测量不规则物体的体积
- django - Django在多个字段中过滤__
- actionscript-3 - 检测字符串中的 URL,并仅使 URL 可点击,该字符串中没有其他内容
- java - 否则,如果未选中单选按钮时循环没有停止,则下一个活动仍然打开
- python - 当我单击带有 selenium 的搜索按钮时,网站会重新加载
- c# - 希望从两个不同层次的嵌套列表中获取信息
- android - Android 计费:更改订阅价格