简体   繁体   中英

Drag SurfaceView in android

I've created a surfaceView that show a camera preview. i want to be able to drag it so the user can move the camera. thats my code, but the dragListener is not responsing:

final SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceview); 
final SurfaceHolder surfaceHolder = surfaceView.getHolder();

buttonStartCameraPreview.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.startcamerapreview)
        {
            Show_Preview(surfaceHolder,surfaceView);
        }               
    }               
});

surfaceView.setOnDragListener(new OnDragListener() {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        camera.stopPreview();
        return false;
    }
});

protected void Show_Preview(SurfaceHolder surfaceHolder,SurfaceView surfaceView) {
    // start cameraPreview on SurfaceHolder
}

Better you read some about Drag And Drop . The surfaceView not Dragging because you need to call surfaceView.startDrag() with the proper arguments. The listener you putted is for getting DragEvents when you drag something in screen.

Some code for example:

ClipData dragData = new ClipData("drag", new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN}, null);

    // Instantiates the drag shadow builder.
    View.DragShadowBuilder myShadow = new View.DragShadowBuilder(surfaceView);

    // Starts the drag

    surfaceView.startDrag(dragData,  // the data to be dragged
            myShadow,  // the drag shadow builder
            null,      // no need to use local data
            0          // flags (not currently used, set to 0)
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM