View Layers
In all versions of Android, views have the ability to draw to the off-screen buffer, including using the view rendering cache or canvas. savelayer (). the off-screen buffer, or layer, has a lot of use. you can use them to improve the performance of complex view animations or combination effects. for example, you can use canvas. savelayer () Achieves fade-out effect. This method will temporarily draw a view to a layer and then combine it back to the screen using a transparency coefficient.
From android3.0 (API level11. the setlayertype () method provides more control capabilities for layer. this method has two parameters: the type of the layer you want to use and an optional paint object, which describes how the layer should be combined. you can use the paint parameter to apply color filtering, or specify the blending mode or opacity to a layer. view can use one of the following three types:
Layer_type_none: The view is drawn in the normal way, and the off-screen buffer is not used. This is the default action.
Layer_type_hardware: If the application is hard accelerated, the view will be drawn to a hardware texture. If the application is not hard accelerated, the behavior of this layer is the same as that of layer_type_software.
Layer_type_software: View is drawn to a bitmap.
Which layer type to use depends on your target:
Performance: Use a hardware layer type to draw a View to the hardware texture. when a view is drawn to a layer, its drawing code is not executed until invalidate () is called. some animations, such as transparent animation, can be applied directly on the layer, which is easy for the GPU.
Visual Effects: Use the hardware or software layer type and a paint to apply visual effects to the view. For example, you can use colormatrixcolorfilter in the background to draw a view in black and white.
Compatibility: Use a software layer type to force view to be drawn in software mode. if a view is hard-accelerated (for example, if your entire application is hard-accelerated), but a problem occurs during presentation, using the software rendering method is the easiest way to avoid the limitation of the hardware rendering pipeline.
View layers and animation
When your application is hard accelerated, the hardware layer can provide faster and smoother animations. when you apply an animation to a complex view with many painting operations, it is impossible to always run at a speed of 60 FPS. however, hardware layers can be used to present the view to the hardware texture to reduce the workload. hardware textures can be used to generate animations to prevent the view from repeatedly repainting itself to produce animations. view will not be re-painted before calling invalidate () or calling invalidate () because its attributes change. If you are running an animation in your application but cannot achieve smooth effect, you can enable the hardware layer on your animation view.
When a view is supported by a hardware layer, the processing of some of its properties is related to the way the layer is mixed to the screen. setting these attributes will become more efficient, because they will no longer make the view invalid and re-paint. these attributes are listed below. When you call the configurator of these attributes, the invalidation operation will be optimized best and the target view will not be re-painted:
ALPHA: changes the transparency of a layer.
X, Y, translationx, and translationy: change the location of the layer.
Scalex and scaley: Change the layer size.
Rotation, rotationx, and rotationy: Change the layer direction in 3D space.
Except Tx, except TY: changes the coordinates of the layer's transformation origin.
These attributes are used when an objectanimator is used to generate an animation for a view. if you want to operate on these attributes, call the appropriate setter or getter. for example, to change the transparency property, call setalpha (). the following code snippet demonstrates the best way to change a view about the Y axis:
view.setLayerType(View.LAYER_TYPE_HARDWARE, null);ObjectAnimator.ofFloat(view, "rotationY", 180).start();
Since hardware layers use the graphics card memory, it is strongly recommended that you use them only during the animation and disable them after the animation ends. You can use the animation listener:
View.setLayerType(View.LAYER_TYPE_HARDWARE, null);ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotationY", 180);animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { view.setLayerType(View.LAYER_TYPE_NONE, null); }});animator.start();
Tips and tips
Switching to 2D graphics Hard acceleration can immediately improve performance, but you still need to follow the suggestions below to effectively use the GPU:
Reduce the number of views in your application
The more views the system draws, the slower it will be. This is also true for the software presentation pipeline. Reducing views is the simplest way to optimize your UI.
Avoid over-Drawing
Do not draw too many layers on each other. remove the view that is completely covered by other non-transparent views. if you need to mix multiple layers to each view, consider merging them into a layer. A good principle for the current hardware is that the number of painting times should not exceed 2.5 times the number of pixels on the screen per frame (transparent pixels are counted by Bitmap ).
Do not create a render object in the drawing method
A common mistake is to create a new paint or path each time you call the painting method. This forces the Garbage Collector to run more frequently and causes high-speed cache and hardware pipeline optimization to be ineffective.
Do not modify shapes too frequently.
For example, when shapes, paths, and circles are mixed, texture masks are used for rendering. each time you create or modify a path, the hardware pipeline creates a new mask, which is very expensive.
Do not modify bitmap too frequently
Every time you change the content of a bitmap, it will be re-uploaded as a GPU texture when you draw it next time.
Use alpha with caution (transparency)
When you use setalpha (), alphaanimation, or objectanimator to set a view to transparent, it will be displayed in an off-screen buffer, and the filling rate needs to be doubled. when applying transparency to a very view, you should consider setting the view layer type to layer_type_hardware.