几天以来,我一直被困在这个问题上。我想要的只是要在屏幕上显示的相机的一部分。这就是我想要的:
这是我所做的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="160dp"
android:scrollbars="none"
android:fillViewport="true"
>
<LinearLayout
android:id="@+id/linearLayoutBeautyContent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/surfaceViewBeautyCamera"/>
</LinearLayout>
</ScrollView>
<TextView
android:id="@+id/scanText"
android:text="Scanning..."
android:layout_height="wrap_content"
android:layout_width="match_parent">
</TextView>
</LinearLayout>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LayoutInflater inflator = getLayoutInflater();
final LinearLayout inflate = (LinearLayout) inflator.inflate(R.layout.main, null);
final SurfaceView surfaceView = (SurfaceView) inflate.findViewById(R.id.surfaceViewBeautyCamera);
final SurfaceHolder holder = surfaceView.getHolder();
WindowManager mW = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int screenWidth = mW.getDefaultDisplay().getWidth();
int screenHeight = mW.getDefaultDisplay().getHeight();
final ViewGroup.LayoutParams slayout = surfaceView.getLayoutParams();
slayout.width = screenWidth;
slayout.height = screenHeight;
surfaceView.requestLayout();
final ScrollView beautyContent = (ScrollView) inflate.findViewById(R.id.scrollView);
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = getCamera();
camera.setPreviewDisplay(holder);
camera.setDisplayOrientation(90);
camera.startPreview();
} catch (IOException e) {
Log.i("Lucy", e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
addContentView(inflate, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
public Camera getCamera() {
try {
final Camera open = Camera.open();
return open;
} catch (Exception ex) {
Log.i("Lucy", ex.getMessage());
return null;
}
}
当我这样做时,我得到:
想法是将 SurfaceView 的大小设置为全屏,但减小 ScrollView 的大小。但我想要的是第一个图像。我做错了什么?
观察:
当我完全向上 ScrollView 时。返回主屏幕并再次打开应用程序。我得到第一张图片。但是,如果我再次滚动并返回主页面返回,我会得到第二张图片。
还有其他策略吗?
附言: 1. 相机顶部有其他 View 也是选项,但由于冗长的原因我不能使用它
最佳答案
问题是元素的高度设置: 您的根元素应该是一个相对布局,然后您可以设置相机 View 的固定高度,并将带有文本的 TextView 设置为与父元素高度匹配,并将 layout_below 设置为 ScrollView 。
关于java - 只获取相机的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25660182/