Pregunta

Estoy tratando de desarrollar un widget personalizado para la previa de la cámara. Me gustaría llenar la pantalla de vista previa de cámara y sacar algunos botones sobre ella.

He intentado crear un widget personalizado de la siguiente manera:

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

}

y modifica el archivo main.xml la siguiente manera:

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

En la clase de actividad que he tratado de vincular el widget de la siguiente manera:

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

Cada vez que intento para depurar la aplicación aparece un mensaje de error y no he descubierto lo que podría estar equivocado.

Realmente agradecería cualquier ayuda con respecto a este problema.

Gracias!

¿Fue útil?

Solución

Es necesario crear la siguiente constructor en SurfaceView para que esto funcione:

CapturePreview(Context context, AttributeSet attrs)

Como se puede saber, en Java constructores no se heredan de la superclase es, sin embargo, este es el constructor que Android intenta invocar cuando se utiliza ese componente en un archivo XML (en contraposición a la definición de su interfaz gráfica de usuario mediante programación). Eso no funcionará, por supuesto, por lo tanto, el mensaje de error, que debería ser mucho más clara, en mi opinión.

El AttributeSet contiene todos los atributos mencionados en la declaración XML, por lo que si usted tiene los atributos personalizados para su componente, puede recuperar y tomar acciones sobre ellas en el constructor.

Otros consejos

Puede implementar un onTouchListener () a la vista respetado. Cuando cada vez que hace clic en el superficie automáticamente el sistema de Android le llevará al método onTouchListener ().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top