使用Viewgroup在Camera Preview模式下放置imageview

时间:2013-02-19 19:52:00

标签: android android-layout

我使用Google的示例在ViewGroup中使用CameraPreview模式。我喜欢它的想法,但我试图在它上面放置视图,相机总是取代它。我删除了相机以查看其下方的图像(意味着正在添加视图,它只是被添加到相机下方,即使我将相机放置在图像下方并且调用了带有孩子的游戏(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);

}
....

有人有什么想法吗?

1 个答案:

答案 0 :(得分:2)

答案是ViewGroup从下往上工作。这意味着0实际上是最顶层的视图,而不是底部。我改变了订单:

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

致:

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

它将图像置于顶部。我花了太多时间来处理这个简单的答案。