Android graphics-hardware acceleration (hardware acceleration) (5)

Source: Internet
Author: User

This article translated from: http://developer.android.com/guide/topics/graphics/hardware-accel.html

View layer and animation

When an application is accelerated by hardware, the hardware layer can deliver faster and smoother animations. It is not always possible to play an animation with complex drawing operations at 60 frames per second. In this way, you can use the hardware layer to display the view object in the hardware texture, which can alleviate this situation. Hardware textures can be used for animated views. In this way, when the view object is animated, The repainting operation required by the view object can be eliminated. The view object is repainted only when its attributes change (the invalidate () method is called. If an animation is running on an application and the expected smooth result is not obtained, You need to enable the hardware layer on the animation view.

When a view is returned by the hardware layer, some properties processed by the layer method are merged to the screen. Because they do not need to invalidate or redraw view objects, setting these attributes is very efficient. The following describes how the affected layer is merged. Calling these attribute setters will result in Optimization of invalid processing and will not re-paint the target view object:

1. ALPHA: changes the transparency of the layer;

2. x, y, translation, and translation: Change the layer position;

3. scalex and scaley: The size changed;

4. rotation, rotation, and rotationy: change the orientation of the 3D space middle layer;

5. Transport Tx, transport TY: the conversion origin of the change layer.

These attributes are the names used when the objectanimator object is used to set an animation for the view object. If you want to access these attributes, call the corresponding set or get method. For example, to modify the Alpha attribute, call the setalpha () method. The following code shows the most effective way to rotate a view object around the Y axis in a 3D space:

View. setlayertype (view. layer_type_hardware, null );
Objectanimator. Offline (View, "rotationy", 180). Start ();

Because the hardware layer consumes Display memory, we strongly recommend that you enable the hardware layer only during animation playback and disable the hardware layer after the animation playback ends. You can use an animation listener to perform this operation:

View. setlayertype (view. layer_type_hardware, null );
Objectanimator animator
= Objectanimator. offloat (View, "rotationy", 180 );
Animator. addlistener (newanimatorlisteneradapter (){
@ Override
Publicvoid onanimationend (animator
Animation ){
View. setlayertype (view. layer_type_none, null );
}
});
Animator. Start ();

For more information about property animations, see property animation (http://developer.android.com/guide/topics/graphics/prop-animation.html)

Tips and tips

Switching to a hardware-accelerated 2D image can effectively improve performance, but we still need to design applications in the following recommended ways to effectively use GPU:

1. Reduce the number of view objects in the Application

The more view objects the system draws, the slower it will be. This situation also applies to the software rendering pipeline. One of the effective ways to reduce view objects is to optimize the UI.

2. Avoid over-Drawing

Do not draw too many layers on top of each other. This is because when you delete a view, you must also delete all other non-transparent view objects on the view object. If you need to draw several layers, try to use the compositing mode above and consider merging them into a layer. A good rule is: the number of frames should not be 2.5 times the number of frames on the screen (calculated based on the number of transparent Bitmap ).

3. Do not create rendering objects in the drawing method.

A common error occurs when a new paint object or path object is created each time the rendering method is called. This forces frequent garbage collection and bypasses the cache and Optimization in the hardware pipeline.

4. Do not edit shapes frequently.

Texture masks are used to present complex shapes, such as paths and circles. Each time a path is created or modified, a new mask is created for the hardware channel, which consumes a lot of resources.

5. Do not edit bitmap frequently.

Each time the bitmap content is changed, the texture of the GPU is updated again at the next drawing.

6. Be careful when using alpha-related methods

When setalpha (), alphaanimation, or objectanimator is used to make a view object translucent, the off-screen cache with double filling rate is required. When applying transparent effects on a large view object, you must set the layer type of the view object to layer_type_hardware.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.