0

I'm working on an Android app and I want to add some eyecandy to the UI. I have an activity (let's call it MainActivity) which has some text fields, buttons and a gallery on it. What I would like to achieve is: When the user touches somewhere on this activity, there should be some visual effect at the point where he touched (e.g. something like sparkles etc.).

So the essential parts of my question are: a) How can I determine where the user touched b) how can I draw my effects on to the screen (as an 'overlay' to the activity).

Thanks in advance for every helpful answer.

(I've checked out this answer but it doesn't seem to be applicable to my situation. )

Community
  • 1
  • 1
gmazlami
  • 684
  • 2
  • 9
  • 18

4 Answers4

2

First, you have to add onTouchListener to your root Layout, then you can get coordinates, where the user has touched the screen. example :

   float x,y;
   rl = (RelativeLayout) findViewById(R.id.root_layout);
   rl.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float x = event.getX();
            float y = event.getY();
                return super.onTouchEvent();
        }

    });
slezadav
  • 6,104
  • 7
  • 40
  • 61
  • Thanks, that answers part a) ! Any idea on how to draw something overlaying the views in the activity? Is there a possibility to have a canvas overlaying my UI? – gmazlami Aug 28 '12 at 08:12
1

You should override the onTouchEvent method of your activity. You can then obtain the coordinates of the touch event:

@Override
public boolean onTouchEvent(MotionEvent event) {

    float touchX = event.getX();
    float touchY = event.getY();

    switch(event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // touch down
        break;
    case MotionEvent.ACTION_MOVE:
        // movement
        break;
    case MotionEvent.ACTION_UP:
        // touch up
        break;
    default:
        // default
    }

    return super.onTouchEvent(event);
}
sdabet
  • 18,360
  • 11
  • 89
  • 158
  • Thanks, that answers part a) ! Any idea on how to draw something overlaying the views in the activity? (b) – gmazlami Aug 28 '12 at 08:07
  • You can for example put ImageView at those coordinates in which you can put your animation or draw an image. That's what I'd try. – slezadav Aug 28 '12 at 08:14
1

To answer further on your question, to have a overlay, look at the APIDemos -> Graphics -> OpenGL ES -> Translucent GLSurfaceView. This will help you to create overlay on your activity. There may be some other example in API demos which will get you some help. Android's API Demos are good set of examples to address some known issues.

VendettaDroid
  • 3,131
  • 2
  • 28
  • 41
0

If you are working on ICS then I recommend you to look at the Developer Options in Settings application. If you are not working with ICS, I believe you can look at the APIDEMOS for touchevents. They draw lines on touches. The code in that, can get you started.

VendettaDroid
  • 3,131
  • 2
  • 28
  • 41