【WP Development】Realize the "Shake" function

【WP Development】Realize the "Shake" function

Although I log in to WeChat once every eight months, I believe you use it more often than I do. WeChat has a "shake" function, which actually uses the accelerometer. I think even the lowest-end mobile phones have this sensor, which is a very basic sensor.

Gravitational acceleration can be used to determine the current orientation of the phone, and can also be used to detect the direction of movement. When the user shakes the phone, motion acceleration is generated.

The Accelerometer class in the Windows.Devices.Sensors namespace encapsulates operations related to the gravity accelerometer. "Shake" can determine the acceleration of the phone in the X, Y, and Z directions. When you shake the phone, you are quite excited. Therefore, the absolute values ​​of the three values ​​of X, Y, and Z will be greater than 1. If you shake it hard, it can reach about 1.7 or 1.8. I don't know if it can reach 2. Anyway, I haven't tried it. It may happen when falling from a high altitude. Interested friends can throw the phone down from the roof and try it.

For the coordinate system when using the sensor, the device coordinates used are the same regardless of the sensor. I have drawn a sketch here, which is not very accurate, but I believe that even a junior high school student can understand it.

[[120978]]

The direction pointed by the arrow is the positive direction, and the opposite direction is the negative direction.

With the above mental preparation, I guess you all know what to do. The principle is to read the data on the X, Y, and Z axes respectively, and then judge their values. If the value is large, it means that the phone is in "passion", that is, the user is shaking the phone.

Not only can you "shake", you can also use this to switch songs in a music player. For example, shaking it to the left plays the previous song, and shaking it to the right plays the next song. To shake left and right, you only need to judge the value on the X-axis. For example, when the value on the X-axis is less than -1.6, it can be considered that the user is shaking the phone desperately to the left; if it is greater than 1.6, it means that the user is shaking the phone to the right. Similarly, the value of the Y-axis is positive when shaking upwards, and the larger the value is; the value on the Y-axis is negative when shaking downwards, and the smaller the value is; when you throw the phone screen towards the sky, the value on the Z-axis is positive, and the value is getting bigger; otherwise, it is getting smaller.

Let me give you an example first. My example is not complicated. I just prepare 10 beautiful photos of girls. When the phone is thrown away, one of the 10 photos will be randomly picked out and displayed.

The Accelerometer class originally has a Shaken event, which occurs when the phone is shaken, but this event is currently only triggered on tablets and not on mobile phones. After all, the RT API has not been fully ported. Although this event is not triggered, we can judge it ourselves through the accelerometer reading.

First, get an instance of the accelerator.

  1. Accelerometer acc = null ;
  2. ........
  3.  
  4. acc = Accelerometer.GetDefault();

It's very simple, a GetDefault can return the relevant instance. Then, set the time interval for reporting real-time data, because if it is shaking, it doesn't need to be too precise. I choose to report once every 200 milliseconds, but it doesn't have to be too fast.

  1. acc.ReportInterval = 200 ;

Next, handle the VisibilityChanged event of the current program window and read the acceleration data only when the window is visible. It is useless to read it when the window is invisible.

  1. Window.Current.VisibilityChanged += OnWindowVisibilityChanged;
  2. ........
  3. private   void OnWindowVisibilityChanged ( object sender, Windows.UI.Core.VisibilityChangedEventArgs e )
  4. {
  5. if (e.Visible)
  6. {
  7. acc.ReadingChanged += acc_ReadingChanged;
  8. }
  9. else  
  10. {
  11. acc.ReadingChanged -= acc_ReadingChanged;
  12. }
  13. }

When a new reading is reported, a ReadingChanged event occurs. By processing this event, you can obtain the readings on the X, Y, and Z axes.

  1. async void acc_ReadingChanged ( Accelerometer sender, AccelerometerReadingChangedEventArgs args )
  2. {
  3. // Multiply the reading by 100 to scale it up for easier comparison  
  4. double x = args.Reading.AccelerationX * 100d;
  5. double y = args.Reading.AccelerationY * 100d;
  6. double z = args.Reading.AccelerationZ * 100d;
  7. System.Diagnostics.Debug.WriteLine( "X= {0:N0}, Y= {1:N0}, Z= {2:N0}" , x, y, z);
  8. await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
  9. async () =>
  10. {
  11. // The specific value can be obtained through experiments  
  12. if (Math.Abs(x) > 145d || Math.Abs(y) > 140d || Math.Abs(z) > 155d)
  13. {
  14. // Display beautiful pictures  
  15. }
  16. });
  17. }

I multiplied all three readings by 100 here just because I think it looks good. If you don’t like it, you don’t have to multiply them by 100.

When the user "shakes", our code does not care in which direction he shakes, as long as he shakes it. Therefore, we can take the absolute values ​​of x, y, and z when judging. The reference numbers used for judgment are not fixed. You can try it yourself and find the right number. Here, when the absolute value of the value on X is greater than 145 (1.45), the absolute value of Y is greater than 140 (1.4), and the absolute value of Z is greater than 155 (1.55), it is considered that the user is shaking the phone.

That’s it, the “shake” function is now available, and you can shake it a few times on the real surface (it’s difficult to test on the simulator), and then you will see MM.

Link to this article: http://www.cnblogs.com/tcjiaan/p/4009105.html

<<:  Interview with Zenny Chen: iOS developers should pay attention to Metal

>>:  14 photos summarize Steve Jobs' life

Recommend

Is the Vernal Equinox the Earth’s exclusive right?

Xinhua News Agency, Tianjin, March 19 (Reporter Z...

B station’s promotion beyond the circle!

On the road to breaking out of the circle, if Bil...

What do you need to do to do bidding hosting?

If there is a very professional bidding promotion...

How to create a “hit product”?

Introduction: As long as the trend is right, ever...

How hard is it for roadside trees to provide shade in the summer?

Street trees provide shade in summer and are a be...

TSMC has manufactured over 1 billion 7nm chips (with original text)

Recently, TSMC announced on its official blog tha...

How to sell popular products in the era of private domain traffic?

What is a hot product? Not only do we need to mak...

Tooth-stuck warning! Be careful when buying these types of mangoes!

(Pictures are from the Internet) Mangoes are not ...

Digital RMB is so popular! Alipay can’t hold back

[[395030]] With the popularity of digital RMB har...

Case analysis: The operation and promotion methods of "Lian Coffee"!

When I was doing a community survey on mini progr...