Android:如何创建自定义相机预览小部件?

时间:2009-06-04 14:58:11

标签: android-widget

我正在尝试为相机预览开发自定义小部件。我想用相机预览填满屏幕并在上面画一些按钮。

我尝试过以下方式创建自定义窗口小部件:

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();
}

}

并通过以下方式修改main.xml文件:

<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>

在Activity类中,我尝试以下列方式绑定窗口小部件:

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);
}

每当我尝试调试应用程序时,都会收到错误消息,但我还没有发现可能出错的地方。

对于这个问题我真的很感激。

谢谢!

2 个答案:

答案 0 :(得分:4)

您需要在SurfaceView中创建以下构造函数才能使其正常工作:

CapturePreview(Context context, AttributeSet attrs)

正如您可能知道的那样,Java构造函数不是从它的超类继承的,但这是Android在XML文件中使用该组件时尝试调用的构造函数(而不是以编程方式定义GUI)。当然,这不会起作用,因此我认为错误信息应该更加清晰。

AttributeSet包含XML声明中提到的每个属性,因此如果您有组件的自定义属性,则可以在构造函数中检索它们并对它们执行操作。

答案 1 :(得分:-1)

您可以将onTouchListener()实现到受尊重的视图。当您自动点击表面时,Android系统会将您带到onTouchListener()方法。