How to add a listener to touch the surface?

I am new to Android, so please excuse me if it is set earlier!

I play with some camera code (found on the Internet) and I want to show / hide some buttons on the screen. When the user touches the screen, I want him to capture the image.

My setup:

1. Main activity:

public class CameraDemo extends Activity {
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_inuse);

        preview = new Preview(this);

        ((FrameLayout) findViewById(R.id.preview)).addView(preview);

... ...  
// rest of the code that captures the image when a button is pressed.
// the button is defined in main.xml with button id ButtonClicked
}

2. The preview class is as follows:

class Preview extends SurfaceView implements SurfaceHolder.Callback {

    SurfaceHolder mHolder;
    public Camera camera;

    Preview(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);
}

My question is:

  • How can I add touch functionality so that the user can touch the preview (say, for a second or just quickly click) and something happens? (Say the image is saved)

  • And the "Next" button will appear on the surface, for example?

+3
source share
1 answer
@Override
public boolean onTouchEvent(MotionEvent event) {
    return super.onTouchEvent(event);
}

, MotionEvent , ( .. ..) , .

+5

Source: https://habr.com/ru/post/1793605/


All Articles