// GesturesDemoActivity.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2012-02-06 File created. // 2015-03-16 Last modification. /* This is the main and only .java file of an Android app called GesturesDemo. With this program it is possible to find out what kind of gesture events are detected when you play with the touch screen. To run this program in Android Studio, you have to set up a project with GesturesDemoActivity as its main activity, and use the package name that is given below. This program does not need any .xml files. So you can accept the .xml files generated by Android Studio. After you have created the project, you can overwrite the created .java file with this file. http://developer.android.com/reference/android/view/GestureDetector.html http://developer.android.com/reference/android/view/MotionEvent.html */ package gestures.demo ; import android.app.Activity ; import android.os.Bundle ; import android.graphics.* ; // Classes Canvas, Color, Paint, RectF, etc. import android.view.View ; import android.view.MotionEvent ; import android.view.GestureDetector ; import android.content.Context ; import java.util.ArrayList ; final class GesturesDemoView extends View implements GestureDetector.OnGestureListener { GestureDetector gesture_detector ; ArrayList text_lines_to_screen = new ArrayList() ; public GesturesDemoView( Context context ) { super( context ) ; gesture_detector = new GestureDetector( context, this ) ; text_lines_to_screen.add( "With this application, you can" ) ; text_lines_to_screen.add( "explore which gesture events" ) ; text_lines_to_screen.add( "take place when you play" ) ; text_lines_to_screen.add( "with the touch screen." ) ; text_lines_to_screen.add( "" ) ; setBackgroundColor( 0xFFFFFFE0 ) ; // Light Yellow } public boolean onDown( MotionEvent motion_event ) { text_lines_to_screen.add( "onDown()" ) ; invalidate() ; return true ; } public boolean onFling( MotionEvent first_down_motion, MotionEvent last_move_motion, float velocity_x, float velocity_y ) { text_lines_to_screen.add( "onFling()" ) ; invalidate() ; return true ; } public void onLongPress( MotionEvent first_down_motion ) { text_lines_to_screen.add( "onLongPress()" ) ; invalidate() ; } public boolean onScroll( MotionEvent first_down_motion, MotionEvent last_move_motion, float distance_x, float distance_y ) { text_lines_to_screen.add( "onScroll()" ) ; invalidate() ; return true ; } public void onShowPress( MotionEvent down_motion ) { text_lines_to_screen.add( "onShowPress()" ) ; invalidate() ; } public boolean onSingleTapUp( MotionEvent up_motion ) { text_lines_to_screen.add( "onSingleTapUp()" ) ; invalidate() ; return true ; } public boolean onTouchEvent ( MotionEvent motion_event ) { gesture_detector.onTouchEvent( motion_event ) ; return true ; } protected void onDraw( Canvas canvas ) { Paint text_paint = new Paint() ; text_paint.setStyle( Paint.Style.STROKE ) ; text_paint.setTextSize( 24 ) ; // We'll print only 10 lines to the screen. // The older lines from the beginning of the list will // be deleted. while ( text_lines_to_screen.size() > 10 ) { text_lines_to_screen.remove( 0 ) ; } for ( int line_index = 0 ; line_index < text_lines_to_screen.size() ; line_index ++ ) { String text_line_to_screen = text_lines_to_screen.get( line_index ) ; if ( text_line_to_screen.equals( "onDown()" ) ) { text_paint.setColor( Color.BLACK ) ; } else if ( text_line_to_screen.equals( "onFling()" ) ) { text_paint.setColor( Color.BLUE ) ; } else if ( text_line_to_screen.equals( "onLongPress()" ) ) { text_paint.setColor( Color.RED ) ; } else if ( text_line_to_screen.equals( "onScroll()" ) ) { text_paint.setColor( Color.GRAY ) ; } else if ( text_line_to_screen.equals( "onShowPress()" ) ) { text_paint.setColor( Color.MAGENTA ) ; } else if ( text_line_to_screen.equals( "onSingleTapUp()" ) ) { text_paint.setColor( Color.CYAN ) ; } canvas.drawText( text_line_to_screen, 20, 48 + 32 * line_index, text_paint ) ; } } } public class GesturesDemoActivity extends Activity { // onCreate() will be called when the activity is first created. public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ) ; setContentView( new GesturesDemoView( this ) ) ; } }