Android acceleration sensor: The implementation of the shake function and android Acceleration

Source: Internet
Author: User

Android acceleration sensor: The implementation of the shake function and android Acceleration

I. Principles:

The shake function in the Android mobile phone is already very common. Recently, I came into contact with this function. The principle is very simple: using an acceleration sensor, when shaking the mobile phone, listen to the changes in the acceleration in all directions, when the acceleration value exceeds the specified sensitivity, the shake function is triggered.


Ii. Used classes:

SensorManager

SensorEventListener


Iii. function implementation:

1. Shake function: I encapsulate the shake function into a ShakeUtils class. You can use the ShakeUtils class according to the instructions in the comments. java is as follows (CTRL + SHIFT + O import for poor packages ):

/*** Shake tool class * usage instructions: * private ShakeUtils mShakeUtils = null; * 1. instantiate the tool class in the Activity that requires the shake function and set the shake listener: * mShakeUtils = new ShakeUtils (this); * mShakeUtils. setOnShakeListener (new OnShakeListener {* public void onShake () {* // here is the operation after a shake trigger *}*}); ** 2. Call the onResume and onPause methods of the tool class in the onResume and onPause methods of the Activity respectively: * mShakeUtils. onResume (); * mShakeUtils. onPause (); **/public class ShakeUtils implement S SensorEventListener {public ShakeUtils (Context context) {mSensorManager = (SensorManager) context. getSystemService (Context. SENSOR_SERVICE);} public void setOnShakeListener (OnShakeListener onShakeListener) {mOnShakeListener = onShakeListener;} public void onResume () {mSensorManager. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER), SensorManager. SENSOR_DELAY_NORMAL );} Public void onPause () {mSensorManager. unregisterListener (this) ;}@ Overridepublic void onAccuracyChanged (Sensor sensor, int accuracy) {}@ Overridepublic void onSensorChanged (SensorEvent event) {int sensorType = event. sensor. getType (); // values [0]: X axis, values [1]: Y axis, values [2]: Z axis float [] values = event. values; if (sensorType = Sensor. TYPE_ACCELEROMETER) {// here you can adjust the shake sensitivity if (Math. abs (values [0])> SENS OR_VALUE | Math. abs (values [1])> SENSOR_VALUE | Math. abs (values [2])> SENSOR_VALUE) {System. out. println ("sensor value =" + "" + values [0] + "" + values [1] + "" + values [2]); if (null! = MOnShakeListener) {mOnShakeListener. onShake () ;}}} public interface OnShakeListener {public void onShake ();} private SensorManager mSensorManager = null; private OnShakeListener mOnShakeListener = null; private static final int SENSOR_VALUE = 14 ;}


2. BaseActivity: To facilitate program expansion, a base class is written for all activities:

public abstract class BaseActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);init();}private void init(){setContentView( );findViews( );getData( );showContent( );}public abstract void setContentView();public abstract void findViews();public abstract void getData();public abstract void showContent();}

3. Use ShakeBeautyActivity: ShakeUtils:

public class ShakeBeautyActivity extends BaseActivity {@Overridepublic void setContentView() {setContentView( R.layout.activity_shake_beauty_layout );}@Overridepublic void findViews() {mShakeImageView = ( ImageView )findViewById( R.id.shakeImgId );initShakeUtils( );}@Overridepublic void getData() {}@Overridepublic void showContent() {}@Overrideprotected void onResume() {super.onResume();mShakeUtils.onResume( );}@Overrideprotected void onPause() {super.onPause();mShakeUtils.onPause( );}private void initShakeUtils(){mShakeUtils = new ShakeUtils( this );mShakeUtils.setOnShakeListener( new OnShakeListener( ) {@Overridepublic void onShake() {setShakeImage( );}});}private void setShakeImage( ){Random random = new Random( );mShakeImageView.setBackgroundResource( mBeautys[ ( Math.abs(random.nextInt( ) ) )%mBeautys.length ] );}private ImageView mShakeImageView = null;private ShakeUtils mShakeUtils = null;private static final int[] mBeautys = new int[]{ R.drawable.beauty_01,R.drawable.beauty_02,R.drawable.beauty_03,R.drawable.beauty_04,R.drawable.beauty_05,R.drawable.beauty_06,R.drawable.beauty_07,R.drawable.beauty_08,R.drawable.beauty_09};}

4. Download the demo:

Shake Demo


In android, how can I use an acceleration sensor to shake the image?

An event is triggered when the player is shaken. You can call the player class if you call the player class. You can play the player with a given uri. Add the permission.
 
Event listening when android "Shake" Ends

It is very easy. It depends on gravity sensing (registration required ). When you enter the shake function, register a listener for changes in the x, y, and Z axes. It's easy to make logic after shaking. I will teach you two ways:
1. Write the handler after the unregister of load force sensing, or bind it to a method.
2. Customize a class to inherit from SensorEventListener and write the logic into the unregister method.

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.