2013-02-19 44 views
2

我使用Google的示例在ViewGroup中使用CameraPreview模式。我喜歡它的想法,但我試圖將視圖置於其上,並且相機始終取代它。我刪除了攝像頭以查看其下方的圖像(意味着正在添加視圖,即使將相機放置在圖像下面並調用了bringChildToFront(myImageView)一百萬次,它也只是添加到相機下方。使用Viewgroup在Camera Preview模式下放置Imageview

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

} 
.... 

任何人有任何想法

回答

2

答案是ViewGroup中自下而上的作品含義0是實際上最上面的展示圖,而不是底部我切換順序:。

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

收件人:

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

它把圖像放在上面。爲了這個簡單的答案,我在這方面花了太多時間。

相關問題