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,
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)
And a button will appear on the surface say a "Next" button, for example?
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With