Lesson 102. Touch - Touch Processing

Lesson 102. Touch – Touch Processing


In this lesson:

– handle the touch

Previously, we used OnClickListener for View components and caught short clicks. Now let’s try to catch the touch and move the finger on the component. They consist of three types of events:

– pressing (finger touched screen)
– movement (finger moves across the screen)
– release (finger torn off the screen)

We will be able to catch all these events in the OnTouchListener handler that we will provide for the View component. This handler gives us a MotionEvent object from which we extract event type and coordinates.

In this lesson, we will only look at single touches. And the multi-touch – in the next lesson.

Let’s create a project:

Project name: P1021_Touch
Build Target: Android 2.3.3
Application name: Touch
Package name: ru.startandroid.develop.p1021touch
Create Activity: MainActivity

strings.xml and main.xml we don’t need, we don’t touch them.

Kodyma MainActivity.java:

package ru.startandroid.develop.p1021touch;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnTouchListener {
  
  TextView tv;
  float x;
  float y;
  String sDown;
  String sMove;
  String sUp;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv = new TextView(this);
        tv.setOnTouchListener(this);
        setContentView(tv);
    }

  @Override
  public boolean onTouch(View v, MotionEvent event) {
    x = event.getX();
    y = event.getY();
    
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: // нажатие
      sDown = "Down: " + x + "," + y;
      sMove = ""; sUp = "";
      break;
    case MotionEvent.ACTION_MOVE: // движение
      sMove = "Move: " + x + "," + y;
      break;
    case MotionEvent.ACTION_UP: // отпускание
    case MotionEvent.ACTION_CANCEL:  
      sMove = "";
      sUp = "Up: " + x + "," + y;
      break;
    }
    tv.setText(sDown + "n" + sMove + "n" + sUp);
    return true;
  }
}

MainActivity implements the OnTouchListener interface to act as a touch handler.

IN onCreate we create a new TextView, let it know that the Touch handler is Activity, and put it on the screen.

The OnTouchListener interface assumes that Activity implements its method onTouch. The method input is a View for which there was a touch event and a MotionEvent object with event information.

The getX and getY methods give us X and Y touch coordinates. The getAction method gives the type of touch event:

ACTION_DOWN – Clicks
ACTION_MOVE – Motion
ACTION_UP – Release
ACTION_CANCEL – almost never happens. As far as I understand it occurs in case of any internal failures and should be treated as ACTION_UP.

In the case of ACTION_DOWN we write in sDown the coordinates of the click.

In the case of ACTION_MOVE, we write the coordinates of the point of the current finger position in sMove. If we move our finger across the screen – this text will be constantly changing.

In the case of ACTION_UP or ACTION_CANCEL, we write in sUp the coordinates of the point where the finger was released.

All this at the end of the event is displayed in TextView. And return true – we handled the event ourselves.

Now we will swipe the screen (cursor over the emulator) in the application, and the screen will see the coordinates of the beginning of the movement, the current coordinates and coordinates of the end of the movement.

We will save everything and launch the application.

Put your finger (cursor) on the screen

If yesterday evening failed, the head does not hurt, the hand is firm and does not tremble :), then the coordinates of pressing.

If the hand shakes, the movement coordinates will also appear.

We continue to move our finger and see how the coordinates of the Move change.

Now we take our finger off the screen and see the coordinates of the point where it happened

In general, everything is easy. When multitouch process is a little complicated, we will already track up to 10 touches.

If you are already familiar with drawing techniques on Android, you can very well create an application that displays a geometric shape that can be moved with your finger. The simplest implementation example can be found here: http://forum.startandroid.ru/viewtopic.php?f=28&t=535.

In the next lesson:

– we handle multiple touches




Discuss in the forum [46 replies]

Leave a Comment