Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [110]

By Root 2579 0
Cursor

If appropriate for your application, adding a custom cursor gives an extra professional polish to your UI.

In the past, you needed to hide the mouse and drag around a sprite, which had negative performance effects. Now you have access to native mouse cursors through the operating system cursor mechanism.

Use the new MouseCursorData class and make its data property a Vector of one or several bitmapData objects. For multiple bitmapData objects, the cursor animates at its own frame rate (if you provide it). The maximum dimension is 32×32. Define its hotSpot property if you only want a part of the cursor to be active.

Refer to the documentation for an example, at http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/ui/MouseCursorData.html.

Asynchronous Bitmap Decoding

An improvement to AIR 2.6 allows the decoding of bitmaps on a separate thread. To take advantage of this, use the LoaderContext class as follows:

import flash.system.LoaderContext;

import flash.display.Loader;

var lc:LoaderContext = new LoaderContext();

lc.imageDecodingPolicy = ImageDecodingPolicy.ON_LOAD;

var loader:Loader = new Loader();

loader.load(new URLRequest("image.jpg", lc);

This improvement also works with Loader.loadBytes().

The asynchronous decoding allows animations to play smoother. For instance, while you have an animation running, you can now load another graphic in the background without concern that the process will temporary interrupt the animation.

Caching Assets

If your application uses dynamic downloaded assets, similar to a Facebook application displaying your contacts picture, consider caching the assets on your device. Doing so will prevent the overhead of a download every time your application initializes.

Save this technique for small files so that you don’t abuse the user’s storage capacity.

Here, we are looking at the name of the file as part of a url. Check if the file already exists on the SD card. If it does, load it locally. If it does not, get the file from a remote server and save it to the device:

import flash.display.Loader;

import flash.net.URLRequest;

import flash.filesystem.File;import flash.filesystem.FileStream;

import flash.filesystem.FileMode;

import flash.net.URLLoader;

import flash.net.URLLoaderDataFormat;

var stream:FileStream;

var fileName:String;

var file:File;

var urlLoader:URLLoader;

var url:String = "http://www.v-ro.com/cat.jpeg";

fileName = new String(url).split("/").pop(); // cat.jpeg

file = File.applicationStorageDirectory.resolvePath(fileName);

if (file.exists) {

var loader:Loader = new Loader();

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLocal);

loader.load(new URLRequest(file.url);

} else {

urlLoader = new urlLoader ();

urlLoader.dataFormat = URLLoaderDataFormat.BINARY;

loader.addEventListener(Event.COMPLETE, onRemote);

loader.load(new URLRequest(url));

}

function onLocal(event:Event):void {

event.currentTarget.removeEventListener(Event.Complete, onLocal);

addChild(event.currentTarget.content);

}

function onRemote(event:Event):void {

event.target.removeEventListener(Event.COMPLETE, onRemote);

var byteArray:ByteArray = event.target.data as ByteArray;

stream.open(file, FileMode.WRITE);

stream.writeBytes(data, 0, data.length);

stream.close();

var loader:Loader = new Loader();

loader.loadBytes(byteArray);

addChild(loader);

}

Components

If you use the Flex framework, try the Tour de Flex application (see Figure 18-4). It is a good starting point for examples using components to develop AIR for Android applications. You can get it from the Android Market.

Figure 18-4. The Tour de Flex application

Flash Builder was initially not suited for mobile development. Components, such as the DataGrid or the Chart, were too complex and too large for the memory footprint.

Some work has been done from the ground up to optimize the framework with Flex Hero. Some components were rewritten to be mobile-optimized. The mobile theme, used when the MobileApplication tag is detected, has larger, touch-friendly

Return Main Page Previous Page Next Page

®Online Book Reader