Heartbeat Effect of Android games

Source: Internet
Author: User
Tags android games
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)

  1. Package com. Ray. test;
  2. Import Android. App. activity;
  3. Import Android. OS. Bundle;
  4. Import Android. OS. vibrator;
  5. Import Android. View. motionevent;
  6. Public class testviberation extends activity {
  7. Vibrator vibrator;
  8. /** Called when the activity is first created .*/
  9. @ Override
  10. Public void oncreate (bundle savedinstancestate ){
  11. Super. oncreate (savedinstancestate );
  12. Setcontentview (R. layout. Main );
  13. }
  14. @ Override
  15. Protected void onstop (){
  16. If (null! = Vibrator ){
  17. Vibrator. Cancel ();
  18. }
  19. Super. onstop ();
  20. }
  21. @ Override
  22. Public Boolean ontouchevent (motionevent event ){
  23. If (event. getaction () = motionevent. action_down ){
  24. Vibrator = (vibrator) getsystemservice (vibrator_service );
  25. Long [] pattern = {800, 50,400, 30}; // off/On/Off/on...
  26. Vibrator. vibrate (pattern, 2); //-1 is not repeated, and non-1 is repeated starting from the specified subscript of Pattern
  27. }
  28. Return super. ontouchevent (event );
  29. }
  30. }

Copy code

<Ignore_js_op style = "word-wrap: Break-word;">

Testviberation.zip

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.