2

我正在使用 Google 的示例通过在 ViewGroup 中使用 CameraPreview 模式。我喜欢它的想法,但我试图在它上面放置视图,而相机总是取代它。我移除了相机以查看它下方的图像(意味着正在添加视图,它只是被添加到相机下方,即使我将相机放在图像下方并调用了一百万次 bringChildToFront(myImageView)。

public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback {
private final String TAG = "Preview";

SurfaceView mSurfaceView;
SurfaceHolder mHolder;
Size mPreviewSize;
List<Size> mSupportedPreviewSizes;
Camera mCamera;
private DrawOnTop mDraw;
public ImageView myImageView;


 CameraPreview(Context context) {
    super(context);
    myImageView = new ImageView(context);
    Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
    Drawable drawable = new BitmapDrawable(bmp);
    myImageView.setImageDrawable(drawable);
    mSurfaceView = new SurfaceView(context);
    addView(mSurfaceView,0);
    addView(myImageView,1); 


    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = mSurfaceView.getHolder();
    mHolder.addCallback(this);



    Log.d("",drawable+"");
    bringChildToFront(myImageView);
    debug(2);
}

public void setCamera(Camera camera) {
    mCamera = camera;
    if (mCamera != null) {
        mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
        requestLayout();
    }

    bringChildToFront(myImageView);

}

public void switchCamera(Camera camera) {
   setCamera(camera);
   try {
       camera.setPreviewDisplay(mHolder);
   } catch (IOException exception) {
       Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
   }
   Camera.Parameters parameters = camera.getParameters();
   parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
   requestLayout();

   camera.setParameters(parameters);
   mSurfaceView.setBottom(0);
    bringChildToFront(myImageView);

}
 public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, acquire the camera and tell it where
    // to draw.
    try {
        if (mCamera != null) {
            mCamera.setPreviewDisplay(holder);
        }
    } catch (IOException exception) {
        Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
    }
    bringChildToFront(myImageView);

}
....

有人有想法么?

4

1 回答 1

2

答案是 ViewGroup 自下而上工作。这意味着 0 实际上是最顶部暴露的视图,而不是底部。我将顺序从:

addView(mSurfaceView,0);
addView(myImageView,1); 

至:

addView(myImageView, 0);
addView(mSurfaceView, 1);

它把图像放在首位。我花了太多时间来解决这个简单的答案。

于 2013-02-19T20:42:26.240 回答