Previous: http://www.bkjia.com/kf/201204/126277.html
Since Android3.0 (API level11), the Android 2D display pipeline has been designed to support Hard acceleration. Hard acceleration uses GPU to perform all the painting operations on the View canvas.
The easiest way to enable Hard acceleration is to enable hardware speed for the entire application. if your application only uses standard view and Drawable, enabling Hard acceleration globally will not have any negative impact. however, since Hard acceleration is not supported by all 2D plotting, enabling it may affect your custom plotting. the problems are often invisible, abnormal, or incorrectly displayed in pixels. to avoid these problems, Android provides the ability to enable or disable Hard acceleration at the following levels:
Application
Activity
Window
View
If your application uses custom rendering, You need to enable Hard acceleration on the real device for testing to find out the problem. the "unsupported painting operations" section describes known painting operations that cannot be hard accelerated and how to avoid them.
Control Hard acceleration
You can control Hard acceleration at the following levels:
Application
Activity
Window
View
Application-level
In your manifest file, add the following attributes to the <application> label to enable Hard acceleration for your entire application:
<Applicationandroid: hardwareAccelerated = "true"...>
Activity-level
If your application performs incorrectly when Hard acceleration is enabled globally, you can enable Hard acceleration for individual activities. to enable or disable Hard acceleration at the actvity level, you can use the android: hardwareAccelerated attribute for the <activity> element. in the following example, Hard acceleration is enabled for the entire application but hard acceleration is disabled for an activity:
<Applicationandroid: hardwareAccelerated = "true">
<Activity.../>
<Activityandroid: hardwareAccelerated = "false"/>
</Application>
Window Level
If you need more granular control, you can use the following code to enable Hard acceleration for a window:
GetWindow (). setFlags (
WindowManager. LayoutParams. FLAG_HARDWARE_ACCELERATED,
WindowManager. LayoutParams. FLAG_HARDWARE_ACCELERATED );
Note: Hard acceleration cannot be disabled at the window level.
Viewlevel
You can use the following code to disable Hard acceleration for individual views during running:
MyView. setLayerType (View. LAYER_TYPE_SOFTWARE, null );
Note: currently, you cannot enable Hard acceleration at the View level. The View layer has other features except hard acceleration.
Determine whether a View can be hard accelerated
Sometimes it is useful for an application to know whether hardware speed is enabled. It is especially important for things like custom views. this is even more important when your application does some custom rendering that is not supported by the latest pipelines.
There are two ways to check whether an application is hard-accelerated:
View. isHardwareAccelerated (): returns true if the View is attached to a hard-accelerated window.
Canvas. isHardwareAccelerated (): returns true if the Canvas is hard-accelerated.
If you have to do this in your drawing code, you should use Canvas. isHardwareAccelerated () instead of View. isHardwareAccelerated (). when a view is attached to a hard-accelerated window, it can still be drawn using a Canvas without any hardware speed. for example, a view is drawn to a bitmap for high-speed cache.
From the column of nkmnkm