2014-10-28 46 views
0

在我的應用程序/活動中,我將相機預覽顯示爲背景(如透明屏幕),在相機預覽的上方,我顯示不同的圖標,但在底部或某些設備右側顯示一個行,但在一些分散 - 它不好顯示。Android Camera Preview不會在整個屏幕上顯示

意味着在底部卵石或右側屏幕相機預覽結束,我將其設置匹配父然後還它顯示這樣的,i的圖像更discribe它,

enter image description here

enter image description here

在底部看到,它顯示的是藍色,它是主佈局文件的背景,我將它設置爲匹配父項,然後在某些設備中將其顯示在底部,並且在一些垂直右側顯示。

我的佈局文件是以下幾點:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#0000ff" > 

    <AnalogClock 
     android:id="@+id/analogClock1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" /> 

    <TextView 
     android:id="@+id/txtDayDate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/analogClock1" 
     android:layout_centerInParent="true" 
     android:textColor="@android:color/white" /> 

    <LinearLayout 
     android:id="@+id/llayoutBottom" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:id="@+id/llayoutBottom" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:layout_marginBottom="15dp" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/btnSettings" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="15dp" 
       android:background="@drawable/setting" /> 

      <Button 
       android:id="@+id/btnGallary" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="15dp" 
       android:background="@drawable/gallary" /> 

      <Button 
       android:id="@+id/btnCamera" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="15dp" 
       android:background="@drawable/camera" /> 

      <Button 
       android:id="@+id/btnPlaystore" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/playstore" /> 
     </LinearLayout> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="7dp" 
      android:layout_marginRight="7dp" 
      android:layout_marginTop="5dp" 
      android:background="@android:color/white" /> 

     <LinearLayout 
      android:id="@+id/llayoutBottom" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:layout_marginBottom="15dp" 
      android:orientation="horizontal" 
      android:visibility="visible" > 

      <Button 
       android:id="@+id/btnCall" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="12dp" 
       android:background="@drawable/call" /> 

      <Button 
       android:id="@+id/btnWeb" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="12dp" 
       android:background="@drawable/web" /> 

      <Button 
       android:id="@+id/btnApp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="12dp" 
       android:background="@drawable/menu" /> 

      <Button 
       android:id="@+id/btnMessage" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="12dp" 
       android:background="@drawable/message" /> 

      <Button 
       android:id="@+id/btnGmail" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/gmail" /> 
     </LinearLayout> 
    </LinearLayout> 

    <GridView 
     android:id="@+id/gridview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:columnWidth="100dp" 
     android:gravity="center" 
     android:horizontalSpacing="10dp" 
     android:numColumns="3" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="10dp" 
     android:visibility="gone" /> 

</RelativeLayout> 

它在java文件中設置如下:

mPreview = new ResizableCameraPreview(this, mCameraId, 
      CameraPreview.LayoutMode.FitToParent, false); 
    LayoutParams previewLayoutParams = new LayoutParams(
      LayoutParams.MATCH_PARENT 
      , LayoutParams.MATCH_PARENT); 
    mLayout.addView(mPreview, 0, previewLayoutParams); 

我儘量說我的問題,它不能很好地問道,但如果任何人給我的解決方案,然後非常感激,謝謝。

回答

0

我用下面的博客找到我的解決方案:

http://android-er.blogspot.in/2010/12/add-overlay-on-camera-preview.html

在下面正在做的:

添加布局XML文件,control.xml,下/ RES /佈局文件夾。它定義了控制層的佈局。

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="bottom" 
    > 
    <Button 
    android:id="@+id/takepicture" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text=" * Take Picture " 
    android:layout_gravity="right" 
    android:layout_margin="10px" 
    /> 
</LinearLayout> 

修改AndroidCamera.java使用control.xml膨脹層

package com.exercise.AndroidCamera; 

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{ 

Camera camera; 
SurfaceView surfaceView; 
SurfaceHolder surfaceHolder; 
boolean previewing = false; 
LayoutInflater controlInflater = null; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

getWindow().setFormat(PixelFormat.UNKNOWN); 
surfaceView = (SurfaceView)findViewById(R.id.camerapreview); 
surfaceHolder = surfaceView.getHolder(); 
surfaceHolder.addCallback(this); 
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

controlInflater = LayoutInflater.from(getBaseContext()); 
View viewControl = controlInflater.inflate(R.layout.control, null); 
LayoutParams layoutParamsControl 
    = new LayoutParams(LayoutParams.FILL_PARENT, 
    LayoutParams.FILL_PARENT); 
this.addContentView(viewControl, layoutParamsControl); 

} 

@Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
    int height) { 
// TODO Auto-generated method stub 
    if(previewing){ 
    camera.stopPreview(); 
    previewing = false; 
    } 

    if (camera != null){ 
    try { 
     camera.setPreviewDisplay(surfaceHolder); 
     camera.startPreview(); 
     previewing = true; 
    } catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
    } 
} 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
// TODO Auto-generated method stub 
camera = Camera.open(); 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
// TODO Auto-generated method stub 
camera.stopPreview(); 
camera.release(); 
camera = null; 
    previewing = false; 
} 
} 

記住授予權限訪問攝像機的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.exercise.AndroidCamera" 
    android:versionCode="1" 
    android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".AndroidCamera" 
      android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

</application> 
    <uses-sdk android:minSdkVersion="4" /> 
    <uses-permission android:name="android.permission.CAMERA"></uses-permission> 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <SurfaceView 
     android:id="@+id/camerapreview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
    /> 
    </LinearLayout>