Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a touch listener to a surface view?

Tags:

android

I am new to Android, so please excuse if it is asked before!

I am playing with some camera code (found online) and I want to show/hide some buttons on screen. When the user touches the screen, I want it 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 looks like this:

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,

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

  2. And a button will appear on the surface say a "Next" button, for example?

like image 591
Droid-Bird Avatar asked Dec 05 '25 22:12

Droid-Bird


1 Answers

@Override
public boolean onTouchEvent(MotionEvent event) {
    return super.onTouchEvent(event);
}

in your preview class , the MotionEvent object will tell you what kind of touch it is (and position etc etc) and let you do whatever you want to do.

like image 67
eephyne Avatar answered Dec 08 '25 13:12

eephyne