Overview

There aren't that many applications currently using RenderScript. Since a picture is worth a thousand words, let's see examples of where RenderScript is used. Perhaps the most popular example is the YouTube application and its carousel view, as shown in Figure 9–1. For clarity, the screenshot was taken when the videos were not loaded yet, and you can see what each rectangle looks like (each rectangle would then contain a video thumbnail). Another example, Balls, is available as sample code in the Android SDK and uses sensors' information and physic to compute the position of the balls on the screen, as shown in Figure 9–2.

Image

Figure 9–1. YouTube Carousel view

Image

Figure 9–2. Balls sample application

NOTE: To create a project from sample code (for example, Balls), create a new Android project in Eclipse and select “Create project from existing sample,” or simply create a new Android Sample project (option available with newer versions of the Eclipse ADT plugin). RenderScript samples are available only when you select 3.0 or above as the target. You can download a carousel example from http://code.google.com/p/android-ui-utils/downloads/list.

RenderScript uses C99 as its language but is not meant to completely replace your Java application. Instead, your application will use RenderScript for certain parts and Java for others. When you use RenderScript, your scripts will be compiled to Low-Level Virtual Machine (LLVM) bitcode on the host machine (for example, your Windows PC on which you installed Eclipse), and the LLVM bitcode will then be compiled to native code on the Android device itself. In that respect, it is similar to your Java code being compiled to Dalvik bytecode on the host, which can be compiled to native code on the device by the Dalvik Just-In-Time compiler.

The native code is then cached on your device, so subsequent executions of the script will be faster as the script will already be compiled to native code. As compilation of the bitcode to native code occurs on the device, the resulting native code can be optimized for the device, or even hardware module (for example, GPU), and you as a developer won't have to worry about the various architectures and features. This is a major difference with the NDK, where, for example, your code has to check whether NEON is available, and should simplify your workflow significantly.

NOTE: For more information about LLVM, visit http://llvm.org. You can download and install various LLVM tools from there as well. LLVM is not an Android-specific technology. As a matter of fact, it is used by Apple's Xcode 4.

Before we talk in more detail about what you can do in RenderScript, we are going to walk through how to create a basic RenderScript script in a project. Since the emulator does not support all the features needed by RenderScript you will need an actual Android device to use RenderScript. You will often find that a RenderScript script is simply referred to as a RenderScript.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.81.162