Code introduction: When the timing in the developed game is about to end, in order to create a tense atmosphere, the heartbeat effect will be simulated by the vibration of the mobile phone itself. In fact, this heartbeat effect is really simple. So you can directly run the code (note that the simulator cannot simulate the vibration, so you have to test it on a real machine)
- Package com. Ray. test;
- Import Android. App. activity;
- Import Android. OS. Bundle;
- Import Android. OS. vibrator;
- Import Android. View. motionevent;
- Public class testviberation extends activity {
- Vibrator vibrator;
- /** Called when the activity is first created .*/
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- }
- @ Override
- Protected void onstop (){
- If (null! = Vibrator ){
- Vibrator. Cancel ();
- }
- Super. onstop ();
- }
- @ Override
- Public Boolean ontouchevent (motionevent event ){
- If (event. getaction () = motionevent. action_down ){
- Vibrator = (vibrator) getsystemservice (vibrator_service );
- Long [] pattern = {800, 50,400, 30}; // off/On/Off/on...
- Vibrator. vibrate (pattern, 2); //-1 is not repeated, and non-1 is repeated starting from the specified subscript of Pattern
- }
- Return super. ontouchevent (event );
- }
- }
Copy code |
<Ignore_js_op style = "word-wrap: Break-word;">
-
Testviberation.zip