アンドロイド:どのようにカスタムカメラのプレビューウィジェットを作成するには?

StackOverflow https://stackoverflow.com/questions/951121

  •  11-09-2019
  •  | 
  •  

質問

私はカメラのプレビュー用のカスタムウィジェットを開発しようとしています。私はカメラのプレビューで画面を埋め、その上にいくつかのボタンを描画したいと思います。

私は次のようにウィジェットカスタムを作成しようとしています:

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);
}
私は、エラーメッセージを取得し、私が間違っている可能性が何かを発見していないアプリケーションをデバッグしようとする

毎回ます。

私は本当にこの問題に関する任意のヘルプをお願い申し上げます。

ありがとうございます。

役に立ちましたか?

解決

あなたが仕事に、このためのSurfaceViewで次のコンストラクタを作成する必要があります:

CapturePreview(Context context, AttributeSet attrs)

あなたが知っているかもしれませんが、

、Javaでコンストラクタは、それはスーパーだから継承されていない、まだこれはAndroidが(プログラムであなたのGUIの定義ではなく)あなたはXMLファイルでそのコンポーネントを使用するときに起動しようとするコンストラクタです。それは、当然のことながら、私の意見では、より多くの明確でなければなりませんので、エラーメッセージが表示され、動作しません。

属性セットには言及したすべての属性が含まれていますあなたは、コンポーネントのカスタム属性を持っているので、もしXML宣言では、あなたが取得することができますし、コンストラクタでそれらの行動をとります。

他のヒント

あなたは尊敬ビューにonTouchListener()を実装することができます。あなたは表面上でクリックすると、これまで自動的にAndroidのシステムはonTouchListener()メソッドが表示されます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top