2009-06-04 10 views
7

Tôi đang cố gắng phát triển tiện ích con tùy chỉnh để xem trước máy ảnh. Tôi muốn điền vào màn hình với xem trước máy ảnh và vẽ một số nút trên nó.Android: Cách tạo tiện ích xem trước máy ảnh tùy chỉnh?

Tôi đã cố gắng để tạo ra một phong tục phụ tùng theo cách sau:

import java.io.IOException; 

import android.content.Context; 
import android.hardware.Camera; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

class CapturePreview extends SurfaceView implements SurfaceHolder.Callback { 
    SurfaceHolder mHolder; 
    Camera mCamera; 

CapturePreview(Context context) { 
    super(context); 
    // Install a SurfaceHolder.Callback so we get notified when the 
    // underlying surface is created and destroyed. 
    mHolder = getHolder(); 
    mHolder.addCallback(this); 
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
} 

public void surfaceCreated(SurfaceHolder holder) { 
    // The Surface has been created, acquire the camera and tell it where 
    // to draw. 
    mCamera = Camera.open(); 
    try { 
     mCamera.setPreviewDisplay(holder); 
    } catch (IOException exception) { 
     mCamera.release(); 
     mCamera = null; 
     // TODO: add more exception handling logic here 
    } 
} 

public void surfaceDestroyed(SurfaceHolder holder) { 
    // Surface will be destroyed when we return, so stop the preview. 
    // Because the CameraDevice object is not a shared resource, it's very 
    // important to release it when the activity is paused. 
    mCamera.stopPreview(); 
    mCamera = null; 
} 

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
    // Now that the size is known, set up the camera parameters and begin 
    // the preview. 
    Camera.Parameters parameters = mCamera.getParameters(); 
    parameters.setPreviewSize(w, h); 
    mCamera.setParameters(parameters); 
    mCamera.startPreview(); 
} 

} 

và sửa đổi các tập tin main.xml theo cách sau:

<RelativeLayout android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout"> 
<dev.video.client.CapturePreview android:id="@+id/capturePreview" android:layout_width="wrap_content" android:layout_height="wrap_content"/> 
</RelativeLayout> 

Trong lớp Hoạt động Tôi đã cố gắng để ràng buộc tiện ích con theo cách sau:

private CapturePreview mPreview; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setFullscreen(); 

    setContentView(R.layout.main); 

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

    //mPreview = new CapturePreview(this); 
    //setContentView(mPreview); 

    mPreview = (CapturePreview)this.findViewById(R.id.capturePreview); 
} 

Mỗi khi tôi cố gắng gỡ lỗi ứng dụng tôi nhận được một thông báo lỗi và tôi đã không phát hiện ra điều gì có thể sai.

Tôi thực sự đánh giá cao bất kỳ trợ giúp nào về vấn đề này.

Cảm ơn!

Trả lời

4

Bạn cần phải tạo các nhà xây dựng sau trong SurfaceView cho điều này để làm việc:

CapturePreview(Context context, AttributeSet attrs)

Như bạn đã biết, trong Java constructor không được thừa kế từ nó là lớp cha, tuy nhiên đây là constructor rằng Android cố gắng gọi khi bạn sử dụng thành phần đó trong một tệp XML (trái với việc định nghĩa GUI của bạn theo chương trình). Điều đó sẽ không hoạt động, tất nhiên, do đó, thông báo lỗi, mà nên được rất nhiều rõ ràng hơn, theo ý kiến ​​của tôi.

AttributeSet chứa mọi thuộc tính được đề cập trong khai báo XML, vì vậy nếu bạn có thuộc tính tùy chỉnh cho thành phần của mình, bạn có thể truy xuất và thực hiện các hành động trên chúng trong hàm tạo.

+0

hi, tôi có cùng một câu hỏi .. nhưng tôi đã nhận được ... plz giúp tôi ... –

-1

Bạn có thể triển khai onTouchListener() cho chế độ xem được tôn trọng. Khi bạn nhấp vào bề mặt tự động, Android System sẽ đưa bạn đến phương thức onTouchListener().