自从几天以来,我就陷入了这个问题。我想要的仅仅是在屏幕上显示相机的一部分。这就是我想要的:获取相机的顶部部分
这是我做了什么:
<?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的大小,全屏但减少滚动视图的大小。但是我想要的是第一张图片。我究竟做错了什么?
观察:
当我完全滚动视图。返回主屏幕并再次打开该应用程序。我得到第一个图像。但如果我再次滚动并返回到主要回来,我得到第二个图像。
任何其他策略?
PS: 1.有相机的顶部另一种观点则也选择,但我不能用它冗长的原因
几个问题:1)为什么你不能使用'setContentView'? 2)为什么要在全屏中使用“SurfaceView”,但是“高度减少的ScrollView”?你想只显示图像的一部分,但能够滚动它吗? – mbmc 2014-09-09 05:26:25
1)我不能使用'setContentView',因为我正在为phonegap构建一个插件。我的观点基本上会在浏览器之上。因此。 2)是。基本上我试图把摄像机的feed放在scrollview中,并将scroller的高度限制为200.这样我只能看到feed的一部分。 – Jatin 2014-09-09 05:38:24
@InfiniteRecursion添加了方法 – Jatin 2014-09-09 09:09:37