Events and gestures
Event handling:
- Physical event handling
- Touch screen event handling
- gesture
Physical event handling
- Based on listening event processing
- Callback event processing
Callback-based time processing: Rewrite Activity callback method
OnTouchEvent + OnKeyDown + OnKeyUp
Based on callback processing events, generalization is adopted. For example, in physical keys
1. Physical Events Provide three events:
Press: OnkeyDown, OnKeyUp: ONKeyLongPress(): Long press
Android provides keyword constants for each physical key in the middle: keyCODE_MENU,KEYCODE_HOME
Case: Press the return key twice in a row to return to the map
package ; import ; import ; import ; import ; /** * Function: Press the return key twice in a row to exit the current application */ public class MainActivity extends AppCompatActivity { private long exitTime = 0; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK){ // Execute the exit method, exit(); return true; } return (keyCode, event); } public void exit() { if( (() - exitTime ) > 2000){ (, "Press again to exit the program", Toast.LENGTH_SHORT).show(); exitTime = (); }else{ finish(); (0); } } }
Touch Events
OnTouchEvent ()
package ; import ; import ; import ; import ; import ; /** * Features: Move hats with Touch to bring hats to penguins * 1. s instance top HatView in the figure shows the hat position * 2. Continuously update X,Y coordinates by listening to OnTouchEvent * 3. Put the hat layout manager, dynamic sky sword */ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); // Add a hat object and touch the event listener final HatView hatView = new HatView(); (new (){ @Override public boolean onTouch(View view, MotionEvent motionEvent) { = ()- 80; // Subtract the hat's normal height and move the center point = (); // New drawing (); return true; } }); RelativeLayout relativeLayout = findViewById(); (hatView); } }
package ; import ; import ; import ; import ; import ; import ; /** * Hat Postition via Touch * */ public class HatView extends View { public float bitmapX; public float bitmapY; public HatView(Context context) { super(context); = 0.0f; = 0.0f; } /** * Repaint the hat * */ @Override protected void onDraw(Canvas canvas) { (canvas); Paint paint = new Paint(); Bitmap bitmap = ((), ); (bitmap,bitmapX,bitmapY,paint); if(()){ (); } } }
Gesture detection
Provide a gesture detection
Gesture Listener must rewrite its six methods, onDown, OnFling (the user's finger drags through the touch screen), OnLongPress
OnScroll, onShowPress, onSingleTapUp (triggered by the user's finger on the screen)
Triggering function for instance users:
package ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Gesture mode sliding animation */ public class MainActivity extends AppCompatActivity implements { ViewFlipper flipper; //Define ViewFlipper GestureDetector detector; //Define gesture detector Animation[] animation = new Animation[4];//Define an animation array and specify switching animations for ViewFlipper final int distance = 50; //Define the minimum distance between two points of gesture action //Define the image array private int[] images = new int[]{.img01, .img02, .img03, .img04, }; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); detector = new GestureDetector(, this); // Add the displayed animation to ViewFlipper and initialize the animation array flipper = findViewById(); for(int i= 0; i< ; i++){ ImageView imageView = new ImageView(this); (images[i]); (imageView); } // Load the animation resource component animation[0] = (this, .slide_in_left); animation[1] = (this, .slide_out_left); animation[2] = (this, .slide_in_right); animation[3] = (this, .slide_out_right); } @Override public boolean onDown(MotionEvent motionEvent) { return false; } @Override public void onShowPress(MotionEvent motionEvent) { } @Override public boolean onSingleTapUp(MotionEvent motionEvent) { return false; } @Override public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { return false; } @Override public void onLongPress(MotionEvent motionEvent) { } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) { //Judge sliding trend by touching coordinates if ((() - ()) > distance){ (animation[2]); (animation[1]); (); return true; }else if (() - () > distance) { // Set a switch animation for flipper (animation[0]); (animation[3]); (); return true; } return false; } @Override public boolean onTouchEvent(MotionEvent event) { return (event); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro xmlns:tools="/tools" android: android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ViewFlipper android:id = "@+id/flipper" android:layout_width="match_parent" android:layout_height="match_parent"> </ViewFlipper> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:andro> <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="500"/> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:andro> <translate android:duration="500" android:fromXDelta="100%p" android:toXDelta="0"/> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:andro> <translate android:duration="500" android:fromXDelta="0" android:toXDelta="-100%p"/> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:andro> <translate android:duration="500" android:fromXDelta="0" android:toXDelta="100%p"/> </set>
Add gestures
Custom gestures:
Getsures Builder gesture app
Summarize:
1. GestureLibararias Load Custom Gesture Library
2. Get GestureOverlayView Set its event listener
3. All gesture predictions from GestureOverlayView are only obtained, and the maximum value is scored.
4. Add the original value from the edito edit box with the gesture of determining the highest value and will be written to the original edit box.
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Gesture mode sliding animation */ public class MainActivity extends AppCompatActivity implements { private GestureDetector detector; private GestureLibrary librarry; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); // Load the custom gesture file, but exit without success librarry = (, ); editText = findViewById(); if(!()){ finish(); } // Get GestureOverlayView Set its event listener GestureOverlayView gestureOverlayView = findViewById(); (); (1000); (); } @Override public void onGesturePerformed(GestureOverlayView gestureOverlayView, Gesture gesture) { // Get its best match and update the edit bar ArrayList<Prediction> predictions = (gesture); int index = 0;// Save the index number of the current prediction double score = 0.0;// Save the score of the current prediction for (int i = 0; i < (); i++) {// Get the best match result Prediction result = (i);// Obtain a prediction result if ( > score) { index = i; score = ; } } String Text = ().toString(); Text += (index).name; (Text); } }
<?xml version="1.0" encoding="utf-8"?>
<EditText android: android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:layout_marginTop="190dp" /> < android: android:layout_width="320dp" android:layout_height="180dp" android:layout_alignParentBottom="true" android:layout_marginBottom="85dp" android:gestureStrokeType="multiple" />
This is the end of this article about detailed explanations of Android events and gesture operations. For more related contents of Android events and gestures, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!