Starting from 3.0 (API Level 11), Android supports hardware acceleration when drawing views, making full use of the GPU characteristics to make drawing smoother, but consumes more memory.
Turn on or off hardware acceleration:
Since hardware acceleration itself is not perfect, Android provides the option to turn hardware acceleration on or off, which is turned off by default. Hardware acceleration can be turned on or off at 4 levels:
Application level: <applicationandroid:hardwareAccelerated="true" ...>
Activity level: <activity android:hardwareAccelerated="false" ...>
Window level:
getWindow().setFlags( .FLAG_HARDWARE_ACCELERATED,.FLAG_HARDWARE_ACCELERATED);
Note: So far, Android does not support turning off hardware acceleration at the Window level.
View level:
(View.LAYER_TYPE_HARDWARE, null);
Note: So far, Android also: it does not support turning on hardware acceleration at the View level.
Detect whether hardware acceleration is currently enabled:
// Method 1// This method returns true, if myView is hung under a Window with hardware acceleration enabled,// That is to say, it does not necessarily use hardware acceleration when drawing, getDrawingCache(); // Method 2// Return true, if canvas is enabled for hardware acceleration while drawing// Try to use this method to determine whether hardware acceleration is enabled();
Control hardware acceleration switch
The introduction says that controlling hardware acceleration can be performed at different levels, so that it can avoid the stupid situation that the entire application cannot use hardware acceleration because a drawing action is not supported. There are four levels in total, from top to bottom:
1. Application
Application level controls hardware acceleration, in which:
<application android:hardwareAccelerated="true" ...>
On Android 4.0 or above (including) it is true by default, which means it is turned on. If the entire application does not want to use hardware acceleration, set to false.
2. Activity
The control method of Activity level is still carried out. The following shows an example where the application uses hardware acceleration as a whole, but an activity does not use it:
<application android:hardwareAccelerated="true"> <activity ... /> <activity android:hardwareAccelerated="false" /> </application>
3. Window
Window-level control, if you just want a window to use hardware acceleration:
getWindow().setFlags( .FLAG_HARDWARE_ACCELERATED, .FLAG_HARDWARE_ACCELERATED);
4. View
It is possible to control whether a single View uses hardware acceleration at runtime through code:
(View.LAYER_TYPE_SOFTWARE, null);
LAYER_TYPE_HARDWARE means using hardware acceleration (GPU), and LAYER_TYPE_SOFTWARE uses CPU to draw.
Understand the drawing model of View:
1. No hardware acceleration: invalidate the view hierarchy -----> draw the view hierarchy
2. There is hardware acceleration: invalidate the view hierarchy ------> record and update the display list ------> draw the display list
Limitations of hardware acceleration:
At present, Android's support for hardware acceleration is not perfect, and some drawing operations do not work properly with hardware acceleration on (for the specific list, please refer to the Android developer documentation).
However, Android can ensure that built-in components and applications support hardware acceleration. Therefore, if only standard UI components are used in the application, you can safely enable hardware acceleration.
With the upgrade of Android version, I believe that hardware acceleration can be perfectly supported after a period of time.
Exceptional reactions after turning on hardware acceleration:
1. Some UI elements are not displayed: it may be that invalidate is not called
2. Some UI elements are not updated: it may be that invalidate is not called
3. Incorrect drawing: An operation that does not support hardware acceleration may be used, and it is necessary to turn off hardware acceleration or bypass this operation.
4. Throw an exception: An operation that does not support hardware acceleration may be used, and it is necessary to turn off hardware acceleration or bypass this operation.