2012-05-31 138 views
1

我正在爲android創建相機應用程序。 我希望我的相機預覽像下面的圖片(例如)。我想將我的相機預覽分成四部分或多部分。 任何一個可以幫我找到這個Android自定義相機預覽?

enter image description here

感謝任何例子或者教程提前

+0

你想要這四個部分的onClick? – Shrikant

+0

我希望在我的相機表面上分四部分進行預覽,並在拍攝照片時保存此效果。 –

回答

2

努力之後,我找到了解決辦法。 Yeaahhhh!

camera.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <FrameLayout 
     android:id="@+id/preview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    </FrameLayout> 
    <LinearLayout 
     android:id="@+id/imageViewLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:visibility="gone" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/img1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:background="@drawable/ic_launcher" /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/img2" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:background="@drawable/ic_launcher" /> 
      </LinearLayout> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/img3" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:background="@drawable/ic_launcher" /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" > 

       <ImageView 
        android:id="@+id/img4" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:background="@drawable/ic_launcher" /> 
      </LinearLayout> 
     </LinearLayout> 
    </LinearLayout> 

    <Button 
     android:id="@+id/clickButton" 
     style="@android:style/Animation.Translucent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="@string/click" /> 

</RelativeLayout> 

您的相機的活動應該是這樣的:我只張貼的一部分,我認爲是你應該集中,可以實現剩下的主要部分。如果你卡在某處,請問我。 首先,你應該從你的活動呼籲:

final Button clickButton = (Button) findViewById(R.id.clickButton); 
      clickButton.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(final View view) { 
        camera.takePicture(null, null, mPicture); 
       } 
      }); 

您onPictureTaken()應該是這樣的:

/** 
    * This will be called after taking picture from camera. 
    */ 
    final transient private PictureCallback mPicture = new PictureCallback() { 
     /** 
     * After taking picture, onPictureTaken() will be called where image 
     * will be saved. 
     * 
     */ 
     @Override 
     public void onPictureTaken(final byte[] data, final Camera camera) { 
      OutputStream outStream = null; 
      try { 
       outStream = new BufferedOutputStream(new FileOutputStream(
         mGetFile)); 
       outStream.write(data); // Write the data to corresponding place. 
       ((FrameLayout) findViewById(R.id.preview)) 
         .setVisibility(View.GONE); 
       ((LinearLayout) findViewById(R.id.imageViewLayout)) 
         .setVisibility(View.VISIBLE); 
       final Options options = new Options(); 
       options.inScaled = true; 
       options.inPurgeable = true; 

       // to scale the image to 1/8 
       options.inSampleSize = 8; 

       Bitmap imageBitmap = BitmapFactory.decodeByteArray(data, 0, 
         data.length, options); 
       ImageView image1 = (ImageView) findViewById(R.id.img1); 
       ImageView image2 = (ImageView) findViewById(R.id.img2); 
       ImageView image3 = (ImageView) findViewById(R.id.img3); 
       ImageView image4 = (ImageView) findViewById(R.id.img4); 

       image1.setImageBitmap(imageBitmap); 
       image2.setImageBitmap(imageBitmap); 
       image3.setImageBitmap(imageBitmap); 
       image4.setImageBitmap(imageBitmap); 
      } catch (FileNotFoundException e) { 
       camera.release(); 
      } catch (IOException e) { 
       camera.release(); 
      } finally { 
       try { 
        outStream.close(); 

       } catch (IOException e) { 
        camera.release(); 
       } 
      } 
     } 
    }; 

這將顯示像你上面提到的一個拍攝的圖像。

謝謝:)

+0

這裏,如果你想預覽4個以上的圖像,那麼最好使用GridLayout,因爲管理線性佈局是一件有點複雜的工作。 – Shrikant