Android acceleration sensor simulated shake effect-android learning Tour (66)

Source: Internet
Author: User

Android acceleration sensor simulated shake effect-android learning Tour (66)
This article mainly introduces the simple usage of android acceleration sensors. It simulates shaking. If the acceleration speed in the three directions of x, y, and z exceeds 15, the Toast will pop up, of course, you can set more complex policies. For example, the Code for judging the interval is as follows:

Public class MainActivity extends Activity {private SensorManager sensorManager; private TextView textView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); Sensor sensor = sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER); sens OrManager. registerListener (sensorEventListener, sensor, SensorManager. SENSOR_DELAY_NORMAL);} private SensorEventListener sensorEventListener = new SensorEventListener () {@ Override public void onSensorChanged (SensorEvent sensorEvent) {float xValue = Math. abs (sensorEvent. values [0]); float yValue = Math. abs (sensorEvent. values [1]); float zvalue = Math. abs (sensorEvent. values [2]); if (xValue> 15 | yVal Ue> 15 | zvalue> 15) {Toast. makeText (MainActivity. this, shake, Toast. LENGTH_LONG ). show () ;}@ Override public void onAccuracyChanged (Sensor sensor, int I) {};@ Override protected void onDestroy () {if (sensorManager! = Null) {sensorManager. unregisterListener (sensorEventListener);} super. onDestroy () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. menu_main, menu); return true;} @ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); // noinspection SimplifiableIfStatement if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}

 

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.