Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [45]

By Root 2483 0
achieve the effect you are looking for:

const FILTER:Number = 0.10;

var high:Number = 0.0;

var oldHigh:Number = 0.0;

function onUpdate(event:AccelerometerEvent):void {

var ax:Number = event.accelerationX;

high = ax - (ax*FILTER) + oldHigh * (1.0 - FILTER);

oldHigh = high;

}

Let’s go over the formula. ax is the new accelerometer value on the x-axis. high is the adjusted value after applying the filter formula, and oldHigh is the cumulated value over time.

ax is reduced by one-tenth of its value. For instance, if ax is equal to 3:

high = ax - (ax*FILTER)

3 - (3*0.10)

3 - 0.30

2.70

Let’s say that initially oldHigh is equal to zero, but over time, it is equal to 2. The following code reduces oldHigh by one-tenth:

oldHigh = oldHigh * (1.0 - FILTER));

2 * (1.0 - 0.10);

2 * 0.90

1.80

The new high value is 4.5:

2.70 + 1.80

4.5

Let’s examine these values over time. Returning to our shake example, to test the code with the high-pass filter, the threshold value is increased:

const THRESHOLD:Number = 5.0;

var isMeasuring:Boolean = false;

var isShaking:Boolean = false;

const FILTER:Number = 0.10;

var high:Number = 0.0;

var oldHigh:Number = 0.0;

function onUpdate(e:AccelerometerEvent):void {

if (isMeasuring) {

return;

}

isMeasuring = true;

var ax:Number = event.accelerationX;

high = ax - (ax*FILTER) + oldHigh * (1.0 - FILTER);

oldHigh = high;

if (Math.abs(high) > THRESHOLD) {

isShaking = true;

}

if (isShaking) {

// we have a shake

}

isMeasuring = false;

}

Using a low-pass filter


The low-pass filter has the effect of changing slowly and stabilizing the input. It is handy for rotating art for a flight simulation or a rowing boat.

The low-pass filter looks as follows:

const FILTER:Number = 0.10;

var low:Number = 0.0;

var oldLow:Number = 0.0;

function onUpdate(event:AccelerometerEvent):void {

var ax:Number = accelerationX;

low = ax * FILTER + oldLow * (1.0 - FILTER);

oldLow = low;

}

Keep in mind that the more you filter the value, the more lag you will get in response because the average converses more slowly to the current value of the sensor.

Conclusion

When developing applications using an accelerometer, draw the user in and keep her interested. It is the seduction of a digital device that feels tactile.

Chapter 9. Camera

The camera is an instrument that teaches people how to see without a camera.

—Dorothea Lange

Now that mobile devices and cameras are one and the same device, everyone is a photographer. With mobile devices running email and SMS applications, everyone is also a publisher.

If you want to create an application that uses or creates pictures, you can do so by accessing some of the Android APIs. For instance, you can access the Gallery application, also called the Media Library, to select and display an image. You can add an image to the Gallery from AIR. You also can open the native camera to take a picture or shoot a video. And as we will discuss in this chapter, you can do all of this from within your AIR application.

The Gallery Application and the CameraRoll Class


The Gallery application is the display for the repository of images located on the SD card and accessible by various applications. Launch it, choose an image, and then select Menu→Share. A list of applications (such as Picasa, Messaging, and Email) appears, a convenient way to upload media from the device to another destination (see Figure 9-1).

Figure 9-1. The Gallery application

The flash.media.CameraRoll class is a subclass of the EventDispatcher class. It gives you access to the Gallery. It is not supported for AIR desktop applications.

NOTE

When the native camera application launches, it looks for the SD card. If your device is connected to your development computer with USB storage turned on, the card is already mounted on this filesystem and is not accessible. Make sure you turn off USB storage to test your application during development.

Selecting an Image


You can test that your device supports browsing the Gallery by checking the supportsBrowseForImage

Return Main Page Previous Page Next Page

®Online Book Reader