Use of the Android accelerometer: shake-shake function implementation

Source: Internet
Author: User

First, the principle of introduction:

The function of shaking in Android phone is already very common, recently contacted this function, the principle is very simple: using accelerometer, when shaking the phone, listen to the acceleration in all directions of change, when the acceleration value exceeds the set sensitivity, then trigger shake function.


Second, the use of the class:

Sensormanager

Sensoreventlistener


Third, the function realization:

1, shake a shake function: I will shake the function encapsulated into a class shakeutils, when used in accordance with the instructions in the note can be used, Shakeutils.java as follows (poor package please ctrl+shift+o import):

/** * Shake Tool class * Instructions for use: * Private shakeutils mshakeutils = null; * 1, in the need to use the shaking function of the activity to instantiate the tool class and set a shake to listen: * mshakeutils = new Shakeutils (this); * Mshakeutils.setonshakelistener (New onshakelistener{* public void Onshake () {*///Here is the action after a shake is triggered *} *}); * * 2, the Onresume and OnPause methods of the tool class are called in the activity's Onresume and OnPause methods: * Mshakeutils.onresume (); * Mshakeutils.onpause (); * */public class Shakeutils implements Sensoreventlistener {public shakeutils (context context) {Msensormanager = (sensorm  Anager) Context.getsystemservice (Context.sensor_service); }public void Setonshakelistener (Onshakelistener onshakelistener) {monshakelistener = Onshakelistener;} public void Onresume () {Msensormanager.registerlistener (this, msensormanager.getdefaultsensor (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) {//This can be adjusted to shake the sensitivity if ((Math.Abs (values[0]) > Sensor _value | | Math.Abs (values[1]) > Sensor_value | | Math.Abs (values[2]) > Sensor_value) {System.out.println ("SENSOR VALUE = =" + "+ values[0] +" "+ Valu                es[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 SENSO R_value = 14;}


2, Baseactivity: In order to facilitate the extension of the program, for all the activity wrote a base class:

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 ();p ublic abstract void Findviews ();p ublic abstract void GetData ();p ublic abstract Voi D showcontent ();}

3, the specific use of 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 (R Andom.nextint ()))%mbeautys.length]);} Private ImageView Mshakeimageview = null;private shakeutils mshakeutils = null;private static final int[] Mbeautys = new I nt[]{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};}

Four, the demo download:

Shake a Demo

Use of the Android accelerometer: shake-shake function implementation

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.