Demo of the custom View series (II) and windmill demo
The windmills of versions 1.0 and 2.0 have an unsatisfactory effect: When the fingers are lifted, the windmills stop rotating, now Version 3 is to let the windmill continue to rotate for a period of time when the finger is lifted. The idea is as follows:
1) Keep turning the windmill for 5 seconds when your fingers are raised
2) Listen to the MotionEvent. ACTION_UP event and record the upTime of the finger lift.
3) capture the MotionEvent. ACTION_UP event and redraw it for five seconds
According to the preceding instructions, the code of RotationView has been changed as follows (onTouchEvent is modified based on version 2:
/** Time when the finger is lifted **/private long upTime = 0;/** time when the finger is lifted */private final long stopTimeDuration = 5000; @ Overridepublic boolean onTouchEvent (MotionEvent event) {int action = event. getAction (); switch (action) {case MotionEvent. ACTION_MOVE: // re-paint continuously with the move of the finger. // This method uses postInvalidate (); break; case MotionEvent In the UI thread itself. ACTION_UP: // re-paint with the move of the finger. upTime = System. currentTimeMillis (); post (new Runnable (){@ Overridepublic void run () {long duration = System. currentTimeMillis ()-upTime; if (duration = stopTimeDuration) {return;} else if (duration <stopTimeDuration) {post (this) ;}// used in a non-UI thread. Invalidate () ;}}); break;} return true ;}
Of course, there is still a problem with this version of windmill: the speed of the windmill turns is consistent and will not change with the speed of finger movement. This will be solved in the next version, for details, see the custom View series (3)