Android starts with 3.0 (API level 11), supports hardware acceleration while drawing view, leverages GPU features, makes drawing smoother, but consumes some more memory.
Turn hardware acceleration on or off:
Since hardware acceleration itself is not perfect, Android offers the option to turn hardware acceleration on or 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:
Copy Code code as follows:
GetWindow (). SetFlags (Windowmanager.layoutparams.flag_hardware_accelerated,windowmanager.layoutparams.flag_ hardware_accelerated);
Note: Android does not yet support turning off hardware acceleration at the window level.
View level:
Myview.setlayertype (view.layer_type_hardware, NULL);
Note: Android is not supported to turn on hardware acceleration at the view level so far.
Detects whether hardware acceleration is currently enabled:
Method one
//This method returns True if MyView hangs under a window that has hardware acceleration enabled,
//That is, it does not necessarily use hardware acceleration when drawing, Getdrawingcache
Myview.ishardwareaccelerated ();
Method two
//returns True if Canvas is enabled for hardware acceleration when drawing
() to determine whether the hardware acceleration canvas.ishardwareaccelerated () is turned on
;
Control Hardware Acceleration Switch
The introduction says that controlling hardware acceleration can be done at different levels, which avoids the folly of the whole application being able to use hardware acceleration because of a rendering action that is not supported. There are a total of four levels, from top to bottom are:
1. Application
application level Control hardware acceleration, in Androidmanifest.xml:
<application android:hardwareaccelerated= "true" ...>
Android4.0 above (included) is true by default, that is, open. Set to False if the entire application does not want to use hardware acceleration.
2. Activity
the control method of activity level is still in Androidmanifest.xml, the following shows the application overall use hardware acceleration, and an activity does not use the example:
<application android:hardwareaccelerated= "true" >
<activity .../>
<activity android: Hardwareaccelerated= "false"/>
</application>
3. Window
window-level control, if you just want to have a window use hardware acceleration:
GetWindow (). SetFlags (
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
4. View
you can control whether a single view uses hardware acceleration through code at run time:
Myview.setlayertype (view.layer_type_software, NULL);
Layer_type_hardware is the use of hardware acceleration (GPU), layer_type_software using the CPU to draw.
Understand the drawing model of the View:
1. No hardware acceleration: Invalidate the View hierarchy------> Draw the View hierarchy
2. Hardware acceleration: Invalidate the View hierarchy------> record and update the display list------> draw the Display list
Limitations of hardware acceleration:
Currently, Android support for hardware acceleration is not perfect, and some drawing operations do not work properly with hardware acceleration turned on (a specific list can refer to the Android Developer documentation).
However, Android can ensure that built-in components and applications support hardware acceleration. Therefore, if you use only standard UI components in your application, you can safely turn on hardware acceleration.
With the Android version upgraded, it is believed that hardware acceleration can be perfectly supported after a while.
turn on abnormal response after hardware acceleration:
1. Some UI elements are not displayed: Invalidate may not be invoked
2. Some UI elements are not updated: Invalidate may not be invoked
3. Drawing is incorrect: You may have used an operation that does not support hardware acceleration, or you need to turn off hardware acceleration or bypass the operation
4. Throw an exception: You may have used an operation that does not support hardware acceleration, or you need to turn off hardware acceleration or bypass the operation