How to take advantage of the pinch-to-zoom feature in your Xperia™ X10 apps – Part 1

Recently Sony Ericsson rolled out an update to the Android™ 2.1 operating system in its Xperia™ X10 phones. One of the important new features in the update is support for a multi-touch gesture called pinch-to-zoom. In this first part of a two-part tutorial, you will learn how to take advantage of the pinch-to-zoom feature in your apps. In the second part of the tutorial, you’ll examine a code example that uses the pinch-to-zoom feature.

The pinch-to-zoom feature allows users to zoom in and get a closer, more detailed view of a display, or zoom out and get a wider field of view. For example, in the side-by-side image below, the image on the left shows a display zoomed out for a wider field of view. The image on the right shows the display zoomed in for a closer view.

The pinch-to-zoom feature lets users zoom out for a wider field of view, or zoom in for a closer view.

Although not initially available in the Android 2.1 support in Xperia™ X10, pinch-to-zoom is now available as an over-the-air (OTA) enhancement. Note however that Xperia™ X10 is not yet fully optimized for multi-touch. There are some cases where a pinch-to-zoom gesture generates unusual behavior. For more information, see the Limitations section in Part 2 of the tutorial.

You can take advantage of the pinch-to-zoom feature in applications that you develop for Xperia™ X10. Two applications that are packaged with Xperia™ X10 already take advantage of the feature: the web browser and Google Maps. There are also other applications that make use of this feature, such as Angry Birds.

Below is a video that demonstrates the pinch-to-zoom feature in Xperia™ X10.

Providing pinch-to-zoom capability in your Xperia™ X10 application
Your Xperia™ X10 application needs to do the following to provide pinch-to-zoom capability:

  • Detect that the user has made a pinch-to-zoom gesture, such as moving two fingers across the screen. This is further explained below in Detecting and getting information about the gesture.
  • Get information about the gesture, such as what the action was and when it took place. This is further explained below in Detecting and getting information about the gesture.
  • Process the gesture, that is, respond to the user’s action, as appropriate. This is further explained below in Processing the gesture.

Detecting and getting information about the gesture
When a user touches the screen, Android creates an Android™ MotionEvent object to record information about the touch event, such as the action the user performed and the time when the user performed it. Your application needs to access the object in order to detect a pinch-to-zoom gesture and get information about it. This requires the application to first import the MotionEvent class as follows:

import android.view.MotionEvent;

Processing the gesture
To process the gesture, you create a callback handler. You can use the callback handler to take actions such as get information about a pinch-to-zoom gesture. However, first you need to register the callback handler using the setOnTouchListener() method of the Android View class, as shown in the following code snippet.

import android.view.View;
import android.view.View.OnTouchListener;

The parameter to view.setOnTouchListener represented by:


is the callback handler, that is, the touch listener that you want to register. The callback handler is attached to a specific view. Touch events that are posted to that view are processed by the associated callback handler.

The callback handler must implement Android’s View.OnTouchListener interface and use the onTouch() method defined in the interface to process the touch events. When you call the onTouch() method, you need to supply as parameters a View object and a MotionEvent object, as shown below.

public class MyPinchZoom extends Activity implements OnTouchListener {

   public boolean onTouch(View v, MotionEvent event) {
      // Process touch events


A user starts a pinch-to-zoom gesture by touching the screen with one finger. This event is represented in the MotionEvent object by the constant ACTION_DOWN. When the user touches the screen with a second finger while continuing to touch the screen with the first finger, that event is recorded in the MotionEvent object by the constant ACTION_POINTER_DOWN. Other constants represent other actions taken during the pinch-to-zoom gesture. Here are the set of action-related constants for the pinch-to-zoom gesture:

ACTION_DOWN The user touched the screen.
ACTION_POINTER_DOWN The user touched the screen with a second finger while continuing to touch the screen with the first finger.
ACTION_MOVE The user moved a finger on the screen.
ACTION_POINTER_UP The user released one finger from the screen while continuing to touch the screen with the other finger.
ACTION_UP The user released the finger from the screen (only only one finger was touching the screen).
ACTION_CANCEL The user cancelled the current gesture.

After you register the callback handler, you can use it to call methods on the MotionEvent object to get information about a pinch-to-zoom related event. For example, the getAction() method returns the type of action performed: up down, move , or cancel. The following code snippet shows a call to the getAction() method.

// Process touch events
      int action = event.getAction();

The lowest 8 bits of the action returned by getAction() represent the action code. To access the action code, you can perform a bitwise AND operation (using the & operator). The two operands of the AND operation are the result returned by getAction() and the MotionEvent constant, ACTION_MASK. The ACTION_MASK constant is a bit mask of the part of the action code that represents the action itself. The following line of code performs a bitwise AND operation on the action returned by getAction() and the ACTION_MASK constant.

int actionCode = action & MotionEvent.ACTION_MASK;

You can get the number of pointers (fingers) associated with the event by calling the getPointerCount() method of the MotionEvent object, as shown below.

int pCount = event.getPointerCount();

You can also retrieve the X and Y coordinates associated with the touch event by calling the getX() and getY() methods of the MotionEvent object, respectively. Here is an example that retrieves first the X coordinate and then the Y coordinate.

float xcoord = event.getX;
float ycoord = event.getY;

By identifying a pointer index as a parameter to getX() and getY(), you can retrieve the X and Y coordinates of a specific pointer. The following example shows how to get the X coordinate and Y coordinate of the first pointer.

Float  xcoord = event.getX(0);   //Get the X coordinate for the first
                                 // pointer
Float  ycoord = event.getY(1);   //Get the Y coordinate for the second
                                 // pointer

More information

Read more

How to take advantage of the pinch-to-zoom feature in your Xperia™ X10 apps – Part 1

WebGL support in the Android™ web browser

Hi, my name is Anders Isberg and I work as researcher within Technology Research at Sony Ericsson. In this article I’m going to give an early preview of the future from an internal prototype project where Sony Ericsson has enabled WebGL support in the Android™ web browser. The article gives a hint of what kind of experiences that application developers will be able to create on smartphones while still maintaining the flexibility and portability of the web application model.

Check out a demo video of this after the jump!

Read more

Unity and Xperia™ PLAY create a two-hit combo for game developers

Sony Ericsson and Unity Technologies recently announced a partnership to bring games to the Xperia™ PLAY through its Union content distribution service. Through this partnership, Sony Ericsson will feature Union games on the Xperia™ PLAY through its games recommender application. Unity has created a special API specific to the Xperia™ PLAY to offer developers an easy route to implementing the Xperia™ PLAY’s gaming controls and optimizing their games for them. Together, both companies will work with their developer bases through online, business development and event-based initiatives, such as the upcoming Game Developers Conference 2011 in San Francisco, CA.

Read more

Announcing the Xperia™ pro and Xperia™ neo

Xperia™ neo and Xperia™ pro video screenshot

Today Sony Ericsson expands Xperia™ family with two new generation Android smartphones, as the Xperia™ neo and the Xperia™ pro was announced at Mobile World Congress in Barcelona, Spain. These smartphones feature cutting edge technology from Sony and an exceptional multimedia experience on the latest version of the Android™ platform – Gingerbread.

Check out our brand new video after the jump!

Read more

Sony Ericsson Xperia™ PLAY – World’s first authentic gaming smartphone

Xperia™ PLAY video screenshot

Today sees the announcement of the Sony Ericsson Xperia™ PLAY, the first all-in-one smartphone and game console. XperiaTM PLAY delivers the smartphone functionality that the most serious power users could need, teamed with the immersive gaming experience that any gamer would want.

As a gaming device, the full-featured PlayStation® Certified Android™ smartphone boasts a 4” screen and superb stereo sound combined with real gaming control keys – directional pad, analogue touch pad, trigger buttons, PlayStation® buttons – fine-tuned for sturdiness and tactility.

Want to take a close look and learn about the performance of Xperia™ PLAY? Check out our new video after the jump.

Read more

Use your Sony Ericsson phone with ANT+ enabled health and fitness products

Marcus Hansson explains more about ANT+.

At Sony Ericsson we are very pleased to announce that we will be the first mobile handset manufacturer to have integrated support for ANT+ technology. This means that you will be able to use your Sony Ericsson phone with a large number of commercial health and fitness products. For example, you can use your phone with a heart rate monitor, a pedometer, or both, when you are out running.

Read more

How the Xperia™ X10 mini was designed

The design of the Xperia™ X10 mini is much appreciated by many people, and this year it was also rewarded the Red dot design award. But what thoughts and ideas was put into the design of the Xperia™ X10 mini?

Well, we hooked up with Matthias Kierysch, colour material designer at Sony Ericsson, to find out more:

Read more about this on Sony Ericsson Developer World.

Read more

“Why Android is not enough”

Karl-Johan Dahlström.
Next week Karl-Johan Dahlström from Sony Ericsson’s Developer World will be talking on the topic “Why Android™ is not enough”, at the Apps World conference in London, UK. Certainly an interesting subject, where the point is that Android is important to Sony Ericsson, but it is not enough to only have Android to be the smart phone market leader.

That is why Sony Ericsson has created the Xperia™ brand, which is based on three pillars: Android, Sony and great Sony Ericsson hardware and software design. At Apps World, Karl-Johan will explain more on how this can be turned into a winning formula. Read more about these thoughts in Karl-Johan’s blog post on the Apps World web site.

The event will take place on 30 Nov -1 Dec at Olympia, London, UK. Read more about the event on the Apps World web site.

Read more

How to enable HD video recording for the Xperia™ X10 with Android update 2.0x

As the roll-out of the Android 2.1 update continues for Xperia™ X10, X10 mini and X10 mini pro users, one of the key features introduced with the update is HD video recording in 720p. Johan Abramsson, Developer Support Engineer at Sony Ericsson, has written a quick tutorial on how you can make use of HD video recording from third party applications.

Read more

Page 7 of 8« First...45678