Wednesday, June 29, 2011

Android Touch


Lars Vogel

Version 0.1
03.06.2011
Revision History
Revision 0.103.06.2011Lars Vogel
Created
Touch in Android
This tutorial describes how to use touch in Android applications with Eclipse.

1. Android Touch

1.1. Android Basics

One of the primary inputs for Android is the touch interface. The standard UI components (View) already support touch without the need of the developer to interfere. This article describes how to implement a touch interface in your Android application.
The base class for touch support is the class "MotionEvent". MotionEvent contains the touch related information. To react to touch events in your view or your activity you override the method "onTouchEvent()". This method must return a boolean value which indicates if the touch event has been consumed (true) or if the framework should still react to the touch event (false). If single input is used you can use the methods getX() and getY() to get the current position. Via getAction() you receive the action which was performed.
Table 1. Sample Table
EventDescription
MotionEvent.ACTION_DOWNNew touch started
MotionEvent.ACTION_MOVEFinger is moving
MotionEvent.ACTION_UPFinger went up
MotionEvent.ACTION_OUTSIDEFinger is leaving the UI component

Multitouch was introduced in Android 2.0 and has been improved in each Android version. This tutorial focus currently on SingleTouch and it is planned to extend this for multitouch.

1.2. Android Basics

The following assumes that you have already basic knowledge in Android development .

2. Singletouch Example

We will demonstrate Singletouch via an example with an own view. Create the Android project "de.vogella.android.touch.single" with the activity "SingleTouchView".
Create the following view class "SingleTouchEventView".

package de.vogella.android.touch.single;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class SingleTouchEventView extends View {
 private Paint paint = new Paint();
 private Path path = new Path();

 public SingleTouchEventView(Context context, AttributeSet attrs) {
  super(context, attrs);

  paint.setAntiAlias(true);
  paint.setStrokeWidth(6f);
  paint.setColor(Color.WHITE);
  paint.setStyle(Paint.Style.STROKE);
  paint.setStrokeJoin(Paint.Join.ROUND);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  canvas.drawPath(path, paint);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  float eventX = event.getX();
  float eventY = event.getY();

  switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN:
   path.moveTo(eventX, eventY);
   return true;
  case MotionEvent.ACTION_MOVE:
   path.lineTo(eventX, eventY);
   break;
  case MotionEvent.ACTION_UP:
   // nothing to do
   break;
  default:
   return false;
  }

  // Schedules a repaint.
  invalidate();
  return true;
 }
}

  

Add this view to your activity.

package de.vogella.android.touch.single;

import android.app.Activity;
import android.os.Bundle;

public class SingleTouchActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(new SingleTouchEventView(this, null));
 }
}
  

If you run your application you will be able to draw on the screen with your finger (or with the mouse in the emulator).

3. Thank you

No comments:

Post a Comment