// GesturesDemoScalingActivity.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2019-09-14 File created based on GesturesDemoActivity.java. // 2019-09-14 Last modification. /* This is an improved version of the app GesturesDemo. This version can detect scaling gestures. */ package gestures.demo.scaling ; 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.view.ScaleGestureDetector ; import android.content.Context ; import java.util.ArrayList ; final class GesturesDemoView extends View implements GestureDetector.OnGestureListener, ScaleGestureDetector.OnScaleGestureListener { GestureDetector gesture_detector ; ScaleGestureDetector scale_gesture_detector ; ArrayList text_lines_to_screen = new ArrayList() ; public GesturesDemoView( Context context ) { super( context ) ; gesture_detector = new GestureDetector( context, this ) ; scale_gesture_detector = new ScaleGestureDetector( 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 onScaleBegin( ScaleGestureDetector detector ) { text_lines_to_screen.add( "onScaleBegin()" ) ; invalidate() ; return true ; } public boolean onScale( ScaleGestureDetector detector ) { text_lines_to_screen.add( "onScale()" ) ; invalidate() ; return true ; } public void onScaleEnd( ScaleGestureDetector detector ) { text_lines_to_screen.add( "onScaleEnd()" ) ; invalidate() ; } public boolean onTouchEvent ( MotionEvent motion_event ) { gesture_detector.onTouchEvent( motion_event ) ; scale_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 ) ; } else if ( text_line_to_screen.equals( "onScaleBegin()" ) ) { text_paint.setColor( 0xFFDC143C ) ; // Crimson } else if ( text_line_to_screen.equals( "onScale()" ) ) { text_paint.setColor( 0xFFB8860B ) ; // DarkGoldenRod } else if ( text_line_to_screen.equals( "onScaleEnd()" ) ) { text_paint.setColor( 0xFF8B0000 ) ; // DarkRed } canvas.drawText( text_line_to_screen, 20, 48 + 32 * line_index, text_paint ) ; } } } public class GesturesDemoScalingActivity extends Activity { // onCreate() will be called when the activity is first created. public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ) ; setContentView( new GesturesDemoView( this ) ) ; } }