我在我的Android应用程序中使用SurfaceView作为我的Activity的一部分(请参阅下面的截图),因为我已经编写了以下代码 -
我的XML代码是---
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/createReviewLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/header"
android:layout_marginTop="-20dp"
android:background="@drawable/home_gradient" >
<ImageView
android:id="@+id/reviewedItemImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.8"
android:scaleType="fitXY"
android:src="@drawable/review_gradient"
android:visibility="gone" />
<com.wikireviews.util.InfiniteViewPager
android:id="@+id/categoryPager"
android:layout_width="match_parent"
android:layout_height="@dimen/two_thirty"
android:background="@color/trending_theme_color" >
</com.wikireviews.util.InfiniteViewPager>
<RelativeLayout
android:id="@+id/layoutButtons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/categoryPager"
android:layout_marginBottom="-30dp"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:paddingBottom="30dp" >
<RelativeLayout
android:id="@+id/writeReviewLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="@color/create_rev_item_clr" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@drawable/pencil" />
<TextView
android:id="@+id/writeReviewTextView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:gravity="center"
android:text="@string/write_a_review"
android:textColor="@color/create_rev_item_txt_clr"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/dictateReviewLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="10dp"
android:layout_below="@id/writeReviewLayout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/create_rev_item_clr" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@drawable/dictate" />
<TextView
android:id="@+id/dictateReviewTextView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:paddingLeft="10dp"
android:text="@string/dictate_review"
android:textColor="@color/create_rev_item_txt_clr"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlVidRevParentLayout"
android:layout_width="match_parent"
android:layout_below="@id/dictateReviewLayout"
android:layout_height="@dimen/one_hundred_twenty"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
<SurfaceView
android:id="@+id/videoSurface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible" />
<RelativeLayout
android:id="@+id/videoReviewLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/create_rev_item_clr" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="16dp"
android:src="@drawable/video" />
<TextView
android:id="@+id/videoReviewTextView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@color/transparent"
android:gravity="center"
android:text="@string/video_review"
android:textColor="@color/create_rev_item_txt_clr"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
<TextView
android:id="@+id/tvLblHoldRec"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:layout_alignLeft="@id/rlVidRevParentLayout"
android:layout_alignRight="@id/rlVidRevParentLayout"
android:textColor="@android:color/white"
android:layout_below="@id/rlVidRevParentLayout"
android:textSize="@dimen/sz_ten"
android:visibility="gone"
android:gravity="center"
android:text="@string/hold_rec_to_record_video" />
</RelativeLayout>
</RelativeLayout>
<include
android:id="@+id/footer"
android:layout_width="wrap_content"
android:layout_height="65dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
layout="@layout/footer" />
<include
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
layout="@layout/create_reviews_header" />
<include
android:id="@+id/createRevProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
layout="@layout/progressbar_medium" />
</RelativeLayout>
我的Java代码是 -
cameraSurfaceView = (SurfaceView) findViewById(R.id.videoSurface);
cameraView = new CameraView();
SurfaceHolder surfaceHolder = cameraSurfaceView.getHolder();
try
{
//surfaceHolder.setFixedSize(cameraSurfaceView.getWidth(), cameraSurfaceView.getHeight());
//surfaceHolder.setSizeFromLayout();
surfaceHolder.addCallback(cameraView);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
catch (Exception e)
{
Log.d("debug", "Another exception");
e.printStackTrace();
}
我搜索了很多,但没有找到任何可接受的解决方案。请帮助。