Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize shake refresh in Android

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to achieve shake refresh in Android", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve shake refresh in Android" article.

Realization method

To achieve the shake refresh function, you need to use the Gravity Accelerator (Accelerometer) here.

First of all, it is necessary to ensure that misoperation will not occur when shaking, refreshing or moving the phone. here, we need to realize the control of the sensor to ensure that what is captured is the shaking operation that the user wants. In addition, when we implement this logic operation, we need to separate it from the UI code. It is recommended not to mix the interface logic code with other code, but to separate it for reuse. So first create a new ShakeEventManager class, which needs to listen for sensor events:

Public class ShakeEventManager implements SensorEventListener {.. }

In order to listen to the sensor, the SensorEventListener interface is implemented here, and then you have to operate the gravity accelerometer and register the class we wrote as an event listener:

Public void init (Context ctx) {sManager = (SensorManager) ctx.getSystemService (Context.SENSOR_SERVICE); s = sManager.getDefaultSensor (Sensor.TYPE_ACCELEROMETER); register ();}

Then implement the register () method:

Public void register () {sManager.registerListener (this, s, SensorManager.SENSOR_DELAY_NORMAL);}

When the refresh event is triggered, some conditions need to be detected to ensure that the user is shaking the phone intentionally:

Acceleration must be greater than a certain critical value

We have to start some fixed acceleration sensor events.

The timing of these events must be within a certain range.

Here, the implementation logic code is written in the onSensorChanged method, which is called when the accelerator value is valid. The step is to calculate the value of this acceleration. Here you also need to know the acceleration values of the three coordinates, and then subtract the component of the value of gravity in the three directions. As explained in the Android official tutorial documentation, first go through a layer of filtering to subtract the component of gravity, and then do another coordinate component processing:

Private float calcMaxAcceleration (SensorEvent event) {gravity [0] = calcGravityForce (event.values [0], 0); gravity [1] = calcGravityForce (event.values [1], 1); gravity [2] = calcGravityForce (event.values [2], 2); float accX = event.values [0]-gravity [0]; float accY = event.values [1]-gravity [1]; float accZ = event.values [2]-gravity [2] Float max1 = Math.max (accX, accY); return Math.max (max1, accZ);}

Take a look at the calcGravityForce method:

/ / Low pass filter private float calcGravityForce (float currentVal, int index) {return ALPHA * gravity [index] + (1-ALPHA) * currentVal;}

After knowing the acceleration value of * *, the previous judgment logic is implemented here:

@ Override public void onSensorChanged (SensorEvent sensorEvent) {float maxAcc = calcMaxAcceleration (sensorEvent); Log.d ("SwA", "MaxAcc [" + maxAcc+ "]"); if (maxAcc > = MOV_THRESHOLD) {if (counter = = 0) {counter++; firstMovTime = System.currentTimeMillis (); Log.d ("SwA", "First mov..") } else {long now = System.currentTimeMillis (); if ((now-firstMovTime) = MOV_COUNTS) if (listener! = null) listener.onShake ();}

From the point of view of the code, the third line calculates the acceleration and compares it with a critical value (line 5). If it is a * shake, save the current time to see if other events have been triggered within a certain period of time. If all the conditions are met, the callback method in the interface is called:

Public static interface ShakeListener {public void onShake ();} Test App

The shake event management has been implemented above, and then we need to create a new simple App to use it. Just create a new simple Activity with a ListView, and then refresh the ListView when you shake it:

Public class MainActivity extends ActionBarActivity implements ShakeEventManager.ShakeListener {.... @ Override public void onShake () {/ / We update the ListView}}

As you can see, the interface is refreshed on the fifth line, because this method has already been called when the user shakes the phone.

* there are some issues to consider: when App stops, we need to log off the listener because it takes a lot of power to listen to events all the time. In addition, when App resumes running, you need to re-register the listener:

Override protected void onResume () {super.onResume (); sd.register ();} @ Override protected void onPause () {super.onPause (); sd.deregister () } the above is about the content of this article on "how to achieve wobble refresh in Android". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report