// GesturesDemoActivity.kt Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2012-02-06 Java file created. // 2021-01-14 Kotlin version created in Android Studio. /* This is the main and only .kt 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.content.Context import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint import android.os.Bundle import android.view.GestureDetector import android.view.MotionEvent import android.view.View import java.util.* internal class GesturesDemoView( context: Context? ) : View( context ), GestureDetector.OnGestureListener { var gesture_detector: GestureDetector var text_lines_to_screen = ArrayList() init { gesture_detector = GestureDetector( context, this ) text_lines_to_screen.add( "With this Kotlin app, 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( 0xFFF5F5F5.toInt() ) // white smoke color } override fun onDown( motion_event: MotionEvent ) : Boolean { text_lines_to_screen.add( "onDown()" ) invalidate() return true } override fun onFling( first_down_motion: MotionEvent, last_move_motion: MotionEvent, velocity_x: Float, velocity_y: Float ) : Boolean { text_lines_to_screen.add( "onFling()" ) invalidate() return true } override fun onLongPress( first_down_motion: MotionEvent ) { text_lines_to_screen.add( "onLongPress()" ) invalidate() } override fun onScroll( first_down_motion: MotionEvent, last_move_motion: MotionEvent, distance_x: Float, distance_y: Float ) : Boolean { text_lines_to_screen.add( "onScroll()" ) invalidate() return true } override fun onShowPress( down_motion: MotionEvent ) { text_lines_to_screen.add( "onShowPress()" ) invalidate() } override fun onSingleTapUp( up_motion: MotionEvent ) : Boolean { text_lines_to_screen.add("onSingleTapUp()") invalidate() return true } override fun onTouchEvent( motion_event: MotionEvent ) : Boolean { gesture_detector.onTouchEvent( motion_event ) return true } override fun onDraw( canvas: Canvas ) { val text_paint = Paint() text_paint.style = Paint.Style.STROKE text_paint.textSize = 28F // 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.removeAt( 0 ) } for ( line_index in text_lines_to_screen.indices ) { val text_line_to_screen = text_lines_to_screen[ line_index ] if ( text_line_to_screen == "onDown()" ) { text_paint.color = Color.BLACK } else if ( text_line_to_screen == "onFling()" ) { text_paint.color = Color.BLUE } else if ( text_line_to_screen == "onLongPress()" ) { text_paint.color = Color.RED } else if ( text_line_to_screen == "onScroll()" ) { text_paint.color = Color.GRAY } else if ( text_line_to_screen == "onShowPress()" ) { text_paint.color = Color.MAGENTA } else if ( text_line_to_screen == "onSingleTapUp()" ) { text_paint.color = Color.CYAN } canvas.drawText( text_line_to_screen, 20F, 48 + 36 * line_index.toFloat(), text_paint ) } } } class GesturesDemoActivity : Activity() { // onCreate() will be called when the activity is first created. public override fun onCreate( savedInstanceState: Bundle? ) { super.onCreate(savedInstanceState) setContentView(GesturesDemoView(this)) } }