Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android view animation and hardware layers

I'm currently trying to work out how to properly use hardware layers when animating views.

I'm implementing a ViewGroup that enables the user to drag a child view, after which I animate it to a position when they release (like how ViewPager will settle on a page). This article states that you should only enable hardware layers for the duration of the animation.

The issue I'm having is that after hardware layers are enabled Android has to create the layers, which takes 70-100ms on a Galaxy Nexus. This means I can't do it immediately before starting the animation, as having the delay between the drag and the animation would be very noticeable. I also can't enable it when starting a drag for the same reason.

Now, this delay is only present the first time hardware layers are created, so ideally I would want them to be created as soon as the views are added to the layout. I've been pointed to View.buildLayer(), but I'm unsure how to approach this.

  • What would be the correct way to achieve this?
  • Are there any methods in my ViewGroup I can override and call buildLayer() on the child views?
  • Can the hardware layer be invalidated in some way, causing another 70-100ms delay? And how do I handle this?
like image 884
SimonVT Avatar asked Sep 04 '12 22:09

SimonVT


People also ask

What are the two different types of view animations in Android?

There are two types of animations that you can do with the view animation framework: Tween animation: Creates an animation by performing a series of transformations on a single image with an Animation. Frame animation: or creates an animation by showing a sequence of images in order with an AnimationDrawable .

Should I turn on hardware acceleration Android?

Hardware acceleration is enabled by default if your Target API level is >=14, but can also be explicitly enabled. If your application uses only standard views and Drawable s, turning it on globally should not cause any adverse drawing effects.

How do I enable hardware acceleration on my Android?

We can enable hardware acceleration at application level by adding “android : hardwareAccelerated” attribute to application tag.

What is Show hardware layers update?

"Show hardware layers updates" is a great developer tool for tracking down this issue. It flashes views green whenever a view renders a hardware layer. It should only flash once when the animation starts (i.e. the initial layer render).


1 Answers

The delay happens when there's no layer in the cache, you should not see this delay for subsequent calls to setLayerType(NONE)/setLayerType(HARDWARE). You could call buildLayer() from onSizeChanged() to force a layer to be built and then put in the cache (call setLayerType(NONE) to move the layer to the cache.)

Note that the delay you are seeing depends greatly on the device you are running on.

The reason why you shouldn't keep layers enabled is that it doubles the amount of drawing work every time the view update. For instance, if you move a ListView into a layer and then scroll the list, each frame update during the scroll animation will cause: (a) the list to repaint into the layer (b) the layer to be drawn on screen. It's extremely wasteful and may cause performance issues depending on the complexity of your UI.

like image 85
Romain Guy Avatar answered Oct 19 '22 09:10

Romain Guy