Monday, May 30, 2011

Introducing ViewPropertyAnimator

[This post is by Chet Haase, an Android engineer who specializes in graphics and animation, and who occasionally posts videos and articles on these topics on his CodeDependent blog at graphics-geek.blogspot.com. — Tim Bray]

In an earlier article, Animation in Honeycomb, I talked about the new property animation system available as of Android 3.0. This new animation system makes it easy to animate any kind of property on any object, including the new properties added to the View class in 3.0. In the 3.1 release, we added a small utility class that makes animating these properties even easier.

First, if you’re not familiar with the new View properties such as alpha and translationX, it might help for you to review the section in that earlier article that discusses these properties entitled, rather cleverly, “View properties”. Go ahead and read that now; I’ll wait.

Okay, ready?

Refresher: Using ObjectAnimator

Using the ObjectAnimator class in 3.0, you could animate one of the View properties with a small bit of code. You create the Animator, set any optional properties such as the duration or repetition attributes, and start it. For example, to fade an object called myView out, you would animate the alpha property like this:

    ObjectAnimator.ofFloat(myView, "alpha", 0f).start();

This is obviously not terribly difficult, either to do or to understand. You’re creating and starting an animator with information about the object being animated, the name of the property to be animated, and the value to which it’s animating. Easy stuff.

But it seemed that this could be improved upon. In particular, since the View properties will be very commonly animated, we could make some assumptions and introduce some API that makes animating these properties as simple and readable as possible. At the same time, we wanted to improve some of the performance characteristics of animations on these properties. This last point deserves some explanation, which is what the next paragraph is all about.

There are three aspects of performance that are worth improving about the 3.0 animation model on View properties. One of the elements concerns the mechanism by which we animate properties in a language that has no inherent concept of “properties”. The other performance issues relate to animating multiple properties. When fading out a View, you may only be animating the alpha property. But when a view is being moved on the screen, both the x and y (or translationX and translationY) properties may be animated in parallel. And there may be other situations in which several properties on a view are animated in parallel. There is a certain amount of overhead per property animation that could be combined if we knew that there were several properties being animated.

The Android runtime has no concept of “properties”, so ObjectAnimator uses a technique of turning a String denoting the name of a property into a call to a setter function on the target object. For example, the String “alpha” gets turned into a reference to the setAlpha() method on View. This function is called through either reflection or JNI, mechanisms which work reliably but have some overhead. But for objects and properties that we know, like these properties on View, we should be able to do something better. Given a little API and knowledge about each of the properties being animated, we can simply set the values directly on the object, without the overhead associated with reflection or JNI.

Another piece of overhead is the Animator itself. Although all animations share a single timing mechanism, and thus don’t multiply the overhead of processing timing events, they are separate objects that perform the same tasks for each of their properties. These tasks could be combined if we know ahead of time that we’re running a single animation on several properties. One way to do this in the existing system is to use PropertyValuesHolder. This class allows you to have a single Animator object that animates several properties together and saves on much of the per-Animator overhead. But this approach can lead to more code, complicating what is essentially a simple operation. The new approach allows us to combine several properties under one animation in a much simpler way to write and read.

Finally, each of these properties on View performs several operations to ensure proper invalidation of the object and its parent. For example, translating a View in x invalidates the position that it used to occupy and the position that it now occupies, to ensure that its parent redraws the areas appropriately. Similarly, translating in y invalidates the before and after positions of the view. If these properties are both being animated in parallel, there is duplication of effort since these invalidations could be combined if we had knowledge of the multiple properties being animated. ViewPropertyAnimator takes care of this.

Introducing: ViewPropertyAnimator

ViewPropertyAnimator provides a simple way to animate several properties in parallel, using a single Animator internally. And as it calculates animated values for the properties, it sets them directly on the target View and invalidates that object appropriately, in a much more efficient way than a normal ObjectAnimator could.

Enough chatter: let’s see some code. For the fading-out view example we saw before, you would do the following with ViewPropertyAnimator:

    myView.animate().alpha(0);

Nice. It’s short and it’s very readable. And it’s also easy to combine with other property animations. For example, we could move our view in x and y to (500, 500) as follows:

    myView.animate().x(500).y(500);

There are a couple of things worth noting about these commands:

  • animate(): The magic of the system begins with a call to the new method animate() on the View object. This returns an instance of ViewPropertyAnimator, on which other methods are called which set the animation properties.


  • Auto-start: Note that we didn’t actually start() the animations. In this new API, starting the animations is implicit. As soon as you’re done declaring them, they will all begin. Together. One subtle detail here is that they will actually wait until the next update from the UI toolkit event queue to start; this is the mechanism by which ViewPropertyAnimator collects all declared animations together. As long as you keep declaring animations, it will keep adding them to the list of animations to start on the next frame. As soon as you finish and then relinquish control of the UI thread, the event queue mechanism kicks in and the animations begin.


  • Fluent: ViewPropertyAnimator has a Fluent interface, which allows you to chain method calls together in a very natural way and issue a multi-property animation command as a single line of code. So all of the calls such as x() and y() return the ViewPropertyAnimator instance, on which you can chain other method calls.


You can see from this example that the code is much simpler and more readable. But where do the performance improvements of ViewPropertyAnimator come in?

Performance Anxiety

One of the performance wins of this new approach exists even in this simple example of animating the alpha property. ViewPropertyAnimator uses no reflection or JNI techniques; for example, the alpha() method in the example operates directly on the underlying "alpha" field of a View, once per animation frame.

The other performance wins of ViewPropertyAnimator come in the ability to combine multiple animations. Let’s take a look at another example for this.

When you move a view on the screen, you might animate both the x and y position of the object. For example, this animation moves myView to x/y values of 50 and 100:

    ObjectAnimator animX = ObjectAnimator.ofFloat(myView, "x", 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat(myView, "y", 100f);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();

This code creates two separate animations and plays them together in an AnimatorSet. This means that there is the processing overhead of setting up the AnimatorSet and running two Animators in parallel to animate these x/y properties. There is an alternative approach using PropertyValuesHolder that you can use to combine multiple properties inside of one single Animator:

    PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 50f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 100f);
ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start();

This approach avoids the multiple-Animator overhead, and is the right way to do this prior to ViewPropertyAnimator. And the code isn’t too bad. But using ViewPropertyAnimator, it all gets easier:

    myView.animate().x(50f).y(100f);

The code, once again, is simpler and more readable. And it has the same single-Animator advantage of the PropertyValuesHolder approach above, since ViewPropertyAnimator runs one single Animator internally to animate all of the properties specified.

But there’s one other benefit of the ViewPropertyAnimator example above that’s not apparent from the code: it saves effort internally as it sets each of these properties. Normally, when the setX() and setY() functions are called on View, there is a certain amount of calculation and invalidation that occurs to ensure that the view hierarchy will redraw the correct region affected by the view that moved. ViewPropertyAnimator performs this calculation once per animation frame, instead of once per property. It sets the underlying x/y properties of View directly and performs the invalidation calculations once for x/y (and any other properties being animated) together, avoiding the per-property overhead necessitated by the ObjectAnimator property approach.

An Example

I finished this article, looked at it ... and was bored. Because, frankly, talking about visual effects really begs having some things to look at. The tricky thing is that screenshots don’t really work when you’re talking about animation. (“In this image, you see that the button is moving. Well, not actually moving, but it was when I captured the screenshot. Really.”) So I captured a video of a small demo application that I wrote, and will through the code for the demo here.

Here’s the video. Be sure to turn on your speakers before you start it. The audio is really the best part.

In the video, the buttons on the upper left (“Fade In”, “Fade Out”, etc.) are clicked one after the other, and you can see the effect that those button clicks have on the button at the bottom (“Animating Button”). All of those animations happen thanks to the ViewPropertyAnimator API (of course). I’ll walk through the code for each of the individual animations below.

When the activity first starts, the animations are set up to use a longer duration than the default. This is because I wanted the animations to last long enough in the video for you to see. Changing the default duration for the animatingButton object is a one-line operation to retrieve the ViewPropertyAnimator for the button and set its duration:

    animatingButton.animate().setDuration(2000);

The rest of the code is just a series of OnClickListenerobjects set up on each of the buttons to trigger its specific animation. I’ll put the complete listener in for the first animation below, but for the rest of them I’ll just put the inner code instead of the listener boilerplate.

The first animation in the video happens when the Fade Out button is clicked, which causes Animating Button to (you guessed it) fade out. Here’s the listener for the fadeOut button which performs this action:

    fadeOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animatingButton.animate().alpha(0);
}
});

You can see, in this code, that we simply tell the object to animate to an alpha of 0. It starts from whatever the current alpha value is.

The next button performs a Fade In action, returning the button to an alpha value of 1 (fully opaque):

    animatingButton.animate().alpha(1);

The Move Over and Move Back buttons perform animations on two properties in parallel: x and y. This is done by chaining calls to those property methods in the animator call. For the Move Over button, we have the following:

    int xValue = container.getWidth() - animatingButton.getWidth();
int yValue = container.getHeight() - animatingButton.getHeight();
animatingButton.animate().x(xValue).y(yValue);

And for the Move Back case (where we just want to return the button to its original place at (0, 0) in its container), we have this code:

    animatingButton.animate().x(0).y(0);

One nuance to notice from the video is that, after the Move Over and Move Back animations were run, I then ran them again, clicking the Move Back animation while the Move Over animation was still executing. The second animation on the same properties (x and y) caused the first animation to cancel and the second animation to start from that point. This is an intentional part of the functionality of ViewPropertyAnimator. It takes your command to animate a property and, if necessary, cancels any ongoing animation on that property before starting the new animation.

Finally, we have the 3D rotation effect, where the button spins twice around the Y (vertical) axis. This is obviously a more complicated action and takes a great deal more code than the other animations (or not):

    animatingButton.animate().rotationYBy(720);

One important thing to notice in the rotation animations in the video is that they happen in parallel with part of the Move animations. That is, I clicked on the Move Over button, then the Rotate button. This caused the movement to stat, and then the Rotation to start while it was moving. Since each animation lasted for two seconds, the rotation animation finished after the movement animation was completed. Same thing on the return trip - the button was still spinning after it settled into place at (0, 0). This shows how independent animations (animations that are not grouped together on the animator at the same time) create a completely separate ObjectAnimator internally, allowing the animations to happen independently and in parallel.

Play with the demo some more, check out the code, and groove to the awesome soundtrack for 16.75. And if you want the code for this incredibly complex application (which really is nothing more than five OnClick listeners wrapping the animator code above), you can download it from here.

And so...

For the complete story on ViewPropertyAnimator, you might want to see the SDK documentation. First, there’s the animate() method in View. Second, there’s the ViewPropertyAnimator class itself. I’ve covered the basic functionality of that class in this article, but there are a few more methods in there, mostly around the various properties of View that it animates. Thirdly, there’s ... no, that’s it. Just the method in View and the ViewPropertyAnimator class itself.

ViewPropertyAnimator is not meant to be a replacement for the property animation APIs added in 3.0. Heck, we just added them! In fact, the animation capabilities added in 3.0 provide important plumbing for ViewPropertyAnimator as well as other animation capabilities in the system overall. And the capabilities of ObjectAnimator provide a very flexible and easy to use facility for animating, well, just about anything! But if you want to easily animate one of the standard properties on View and the more limited capabilities of the ViewPropertyAnimator API suit your needs, then it is worth considering.

Note: I don’t want to get you too worried about the overhead of ObjectAnimator; the overhead of reflection, JNI, or any of the rest of the animator process is quite small compared to what else is going on in your program. it’s just that the efficiencies of ViewPropertyAnimator offer some advantages when you are doing lots of View property animation in particular. But to me, the best part about the new API is the code that you write. It’s the best kind of API: concise and readable. Hopefully you agree and will start using ViewPropertyAnimator for your view property animation needs.

fruit ninja android

want to get fruit ninja in your mobile just just copy the link and download it given downhere 







download it here


http://megaupload.com/?d=QDSO3O8V

angry birds rio android

want to download angry birds in your mobile just copy and paste the link given down here 







download it here



http://hotfile.com/dl/115352580/0f70d87/Angry_Birds_Rio.apk

Talking Tom Cat Free


want to get tom cat in your mobile just just copy the link and download it given downhere 





 http://hotfile.com/dl/110872203/7186866/tom_Talking_Tom_Cat_1.2.4.apk

google maps android



want to get google maps in your mobile just just copy the link and download it given downhere 









download it here

http://hotfile.com/dl/89987170/58244a5/Google_Maps_5.0.apk

angry birds android



want to get angry birds  in your mobile just just copy the link and download it given downhere 








download it here



http://megaupload.com/?d=NTOISXX8

BlackBerry Curve 9370 a.k.a. Apollo poses for a photo shoot


BlackBerry has some hard time keeping its still unannounced phones private. The BlackBerry Curve 9370, previously known as Apollo, leaks today in series of shots and a lengthy demo video.
The Curve 9370 a.k.a. Apollo has leaked twice before - via a BlackBerry OS7 ROM and later with anpress-quality picture and specifications.
Today the guys over Tinhte.vn brought us another surprise - lots of excellent live shots of the BlackBerry Curve 9370. See for yourself:

   
BlackBerry Curve 9370


But that's not everything they've got in the bag for us. You can enjoy quite a thorough video demonstrating both the hardware and software side of things.
What we know so far is the Curve 9370 will pack an 800MHz processor, a HVGA screen, 512MB RAM, 5 megapixel camera with LED flash and a complete connectivity package with Wi-Fi N and NFC. It runs on BlackBerry OS7.
   
BlackBerry Curve 9370
We guess the official announcement should happen soon. According to the source, the Curve 9370 is scheduled for an August release.
  
BlackBerry Curve 9370

Asus unveils Padphone droid with tablet dock, MeMO 3D tablet


At Computex, Asus announced a phone with half a tablet and a tablet with half a phone. The Padphone that leaked earlier today is official, although its specs haven’t been finalized yet and then there’s the MeMO tablet with a glasses-free 3D screen and phone-like Bluetooth gadget.
Leaked photos of the Asus Padphone barely entered the rumor mill and it’s already official (this happens a week after theinitial teaser). Asus showed only a mockup at Computex as the design for the gadget hasn’t been finalized yet.
Here’s the deal (if the name hasn’t given it away yet) – it’s an Android phone with 4.3” screen that inserts into a 10.1” tablet. Not much is clear about the Padphone, aside from Asus hinting it will run Ice Cream Sandwich, which makes sense since that’s the Android version that is supposed to unify the phone and tablet Android branches.
Anyway, the tablet is just a dock – it displays the phone’s UI on its bigger screen and provides a big battery, speakers and some extra ports. The phone has a microHDMI and microUSB ports that hook up to the tablet to output the screen image and charge the phone and there’s a 5MP camera on the back (with a hole at the back of the tablet dock so that you can still take photos).


Here are a couple of promo videos that show the Asus Padphone (computer generated) action (remember it’s still at the mockup stage).
Hey wait, was that Internet Explorer 8 on the tablet dock’s screen on this second video (1:36)? What?
We’ve already seen a netbook dock for an Android phone (Motorola’s Atrix and dock) and Asus’ own Transformer tablet sort of turns into a netbook with its keyboard dock. Now the Asus Padphone is another variation on the theme. Asus say it should be out for Christmas and judging by the Transformer’s price tag, there’s a chance it will have a decent price too.
Next up is the Asus Eee Pad MeMO 3D. It’s a 7” Android tablet with a glasses-free 3D IPS display with 1280 x 800 pixels resolution. Sounds impressive, doesn’t it? It reportedly runs Android Honeycomb on a 1.2GHz dual-core Qualcomm CPU. Unlike the Padphone, Asus showed an actual working prototype.
Here’s the deal though – there’s a Bluetooth remote that can control the MeMO 3D and you can actually take calls on it like a phone. It’s got a transparent screen too, to increase the “cool” factor. Of course, if you want to use the remote (called “MeMIC”) as a phone, the MeMO 3D will have to be in Bluetooth range (10m at best).
Engadget shot a hands-on video with the Eee Pad MeMO 3D, and here it is:

HTC device, running WP7 and sporting a 12 MP snapper


Eldar Murtazin has gotten hold of a yet unknown HTC device, running WP7 and sporting a 12 MP snapper on its back. He's even prepared two photos of the phone along with a video demo of the Mango 7.1 update, running on this HTC phone. You can watch it below, if you're curious.

The phone itself shares some looks with the HTC Trophy, namely the huge earpiece grill HTC seems to like so much and the overall form factor. Also there's a thin silver trim running around the display to add to the overall premium look of the handset.


The photos show the camera app on the unknown HTC device, clearly proving it has indeed a 12 MP sensor inside. That would mean that Windows Phone 7 will finally up the ante in the camera department and second that HTC may be on its way to producing a decent cameraphone (finally).
When we got wind of this new handset by HTC, HTC Bresson immediately came to mind, but that one was rumored to bring a 16 MP camera to the megapixel race track. Obviously, HTC won't stop at 12MP.
As for the Mango update, it looks to be working very smoothly on the HTC X. There's a walkthrough of the entire UI but to get the explanations, you'd need to dust off your Russian.
PS: Eldar Murtazin had this to say on his Twitter account: "I like 12 megapixel camera on WP7 - support RAW, some additional settings. Quality is not bad at all. Will compare with N8 tomorrow". Excited? Yeah we are too and will keep you posted.

Samsung Galaxy S II, fastest selling smartphone in Korea


The Samsung Galaxy S II has already been blazing a trail in its homeland of South Korea, but its just passed another impressive landmark. It had already surpassed all previous pre-orders for Samsung in Korea and had doubled that of the iPhone 4.
Now, in its first month alone its sold over 1 million units which makes it the fastest selling smartphone ever in Korea. It's predecessor the Galaxy S took a full 70 days to hit that magic number and the S II promised great things early on when it passed 100,000 units by day three, doubled that by day eight and hit half a million in two weeks.








Part of the S II's one-upmanship over iPhone 4 came about as the Galaxy S II didn't suffer carrier support problems. KT were originally the only carrier for the iPhone until this month when SK picked it up as well. Despite this, it's still a great success for Samsung and one they no doubt hope to reproduce elsewhere with their S II family in the US and the S II's availability across Europe.

Samsung Galaxy S II I9101 spotted in the wild, could be the NFC one



Another version of the Samsung's Galaxy S II has leaked, this time in a bunch of blurry photos. It goes under I9101 model name and looks just like the I9100 Galaxy S II. It could be the NFC version that's supposed to ship in June to UK and other select markets to support the introduction of NFC services.
For all we know, in addition to the original I9100 Galaxy S II smartphone, Samsung is preparing two more rumored versions to expand its market expansion. The first one is the I9103, which should be based on the Tegra2 chipset, while the other one is the NFC-capable Galaxy S II.




Since we know nothing about the I9101, we suppose it should be the NFC-enabled Galaxy S II. The forum user who posted the pictures claims the device is stuck on the boot screen, so there is no information on its specs.
Anyway, at least we know there is another Galaxy S II on the way and we guess it should be announced soon.
You can check out this video that shows the NFC-enabled version of the Galaxy S II in action:
In case you don't to wait the NFC-capable Galaxy S II, you could always get the standard edition and give it the sticker treatment.

Sony Ericsson Xperia X10 gets an update, not the one you expected


Sony Ericsson has just released another update for its Xperia X10 former flagship. It’s not the Gingerbread release everyone was hoping for, but it seems to be a pretty major one nonetheless. There are plenty of changes brought and all early adopters claim to notice a substantial improvement in performance.
The update is version number is 2.1.B.0.1 and it seems to boost screen responsiveness and general performance with the effects being most prominent in Timescape. There’s also a new baseband in the package, which reportedly benefits signal strength and in-call sound quality. Switching between 2G and 3G networks is now said to be faster and initiating a call takes shorter time.

Some have noticed a change in battery life for the better (but then others claim that it’s worse) and there’s now voice search integration just about anywhere where you can need it – search bar, browser etc.
A pretty major change is the system-wide availability of multi-touch. The X10 got official multi-touch support with its previous update, but that only worked in some of the apps.
On the visual side of things Sony Ericsson introduced minor tweaks to the notification area, dialer pad and status bar. New animations have also appeared around the interface and they seem to be smoother than before.
Hackers will be pleased to know that the rooting solution from previous ROMs is compatible with this new one, so you won’t be losing control over your system by updating.
So what are you waiting for – go check out if the update has become officially available to your area. If it hasn’t you can always follow the source link to the XDA-developers forum, where you can find instructions on how to flash it manually. Mind you, you should only do this if you really know what you are doing.

ASUS Padfone leaks before its official launch



This is certainly on of the few devices released lately, whose main highlight is located on its back. Say hello to the ASUS Padfone, people - the company's first smartphone with a tablet included as an accessory.


As you can see from the picture above, the package consists of two pieces - an Android running ASUS phone which docks inside a tablet when necessary.

The full spec sheet of the device/s will be unveiled at the Computex show beginning tomorrow, or in the coming days. Currently there is no info on them. The entire concept of a phone which gets plugged into a tablet though, is more than enough to get us seriously excited.

Earlier this week, ASUS teased us with an image of the upcoming Padfone which left us wondering what it might be. Now that we already know, there is nothing left but to wait for the official announcement and the full spec sheet of this exciting new Android device.

Blackberry Torch 2 UPDATE, more images and video footage


Following on from Thursday's Blackberry Torch 2 spy shots, this latest offering gives us a few more sharper images that really show off in greater detail what we can expect from the look of the device. Not only that, the boys at Techno Buffalo have managed to give it a quick video walk-around too.
   
To reiterate, the Torch 2 is really an exercise in hardware. The design is almost unchanged from theoriginal say for the metallic body panels and the higher resolution 3.2" 480 x 640 display on the outside. Its processor runs at a far more acceptable 1.2GHz clock, paired with an upped 768MB of RAM and internal storage checks in at 8GB with expandable memory via an SD card. The Torch 2 shares the original's 5MP camera but video recording has now been boosted to 720p HD as well and the back panel also houses an NFC chip now too.
   
Proof of BB OS 7 • Keyboard • 5MP Camera with LED Flash • The lock and mute keys
Using the Torch 2 most notably shows off the new Blackberry OS 7 which the video below demonstrates.