BKJIA exclusive Article: Starting from Android 3.0, the Android 2D drawing process is designed to better support hardware acceleration. Hardware acceleration is used when you use a GPU View to draw rows on the Canvas. In the latest Android version, graphics and hardware acceleration and rendering techniques have been improved. BKJIA has made a special assignment to the best web webmaster as the author of this site's column and has written Android 4.0 development articles for all netizens.
For Android 4.0
1. Use of Android 4.0 hardware acceleration
1.1 control level of hardware acceleration
The simplest way to enable hardware acceleration is to enable the global settings of hardware acceleration for the entire system. If your program is a standard View or Drawable, the global setting of hardware acceleration will not cause adverse effects. However, hardware acceleration does not support all 2D painting operations, so enabling hardware acceleration may affect applications that use custom components, the problem is often manifested in invisible element exceptions and incorrect pixel rendering. To solve this problem, Android allows you to choose to start or disable the following levels of hardware acceleration: Application Activity Window and View.
1.1.1 Application level
Add property tags to your Android Manifest file to enable hardware acceleration for the entire application.
1.1.2 Activity level
If your Application cannot perform well at the Application level, you can use the Activity for separate control. To enable or disable hardware acceleration for an Activity, you can use the android: hardwareAccelerated attribute of the activity. The following column enables hardware acceleration for the entire Application, but hardware acceleration is not allowed for an Activity.
1.1.3 Window level
If you need more fine-grained control, you can use the following code to accelerate the window.
- getWindow().setFlags(
- WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Note: currently, you cannot disable hardware acceleration for Windows.
1.1.4 View level
We can disable hardware acceleration for a separate View during runtime. We can use the following code:
- myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Note: currently, hardware acceleration cannot be performed at the View level.
1.2 determine whether a View has enabled hardware acceleration
Sometimes we need to know whether an application has enabled hardware acceleration, especially for some custom controls. Because your application does a lot of custom "painting" operations, but not all processes support the new "painting" rendering process.
There are two different ways to check whether the Application has enabled hardware acceleration:
1.2.1 use View. isHardwareAccelerated (). If true is returned, hardware acceleration is enabled in the View window.
1.2.2 Canvas. isHardwareAccelerated () if true is returned, hardware acceleration is enabled for the Canvas.
If you have to check whether your painting code has been accelerated, use Canvas. isHardwareAccelerated () instead of View. isHardwareAccelerated () if possible (). When a View exists on an accelerated Windows server, you can use a Canvas without hardware acceleration to paint it. For example, when we draw a View to Bitmap and use it as a cache.