文档地址:
http://developer.android.com/guide/topics/graphics/hardware-accel.html
工作太忙没时间翻译,贴出来方便不能翻墙的同学查阅
Beginning in Android 3.0 (API level 11), the Android 2D rendering pipeline supports hardware acceleration, meaning that all drawing operations that are performed on a View
's canvas use the GPU. Because of the increased resources required to enable hardware acceleration, your app will consume more RAM.
Hardware acceleration is enabled by default if your Target API level is >=14, but can also be explicitly enabled. If your application uses only standard views and Drawable
s, turning it on globally should not cause any adverse drawing effects. However, because hardware acceleration is not supported for all of the 2D drawing operations, turning it on might affect some of your custom views or drawing calls. Problems usually manifest themselves as invisible elements, exceptions, or wrongly rendered pixels. To remedy this, Android gives you the option to enable or disable hardware acceleration at multiple levels. See Controlling Hardware Acceleration.
If your application performs custom drawing, test your application on actual hardware devices with hardware acceleration turned on to find any problems. The Unsupported drawing operations section describes known issues with hardware acceleration and how to work around them.
Controlling Hardware Acceleration
You can control hardware acceleration at the following levels:
- Application
- Activity
- Window
- View
Application level
In your Android manifest file, add the following attribute to the <application>
tag to enable hardware acceleration for your entire application:
<application android:hardwareAccelerated="true" ...>
Activity level
If your application does not behave properly with hardware acceleration turned on globally, you can control it for individual activities as well. To enable or disable hardware acceleration at the activity level, you can use the android:hardwareAccelerated
attribute for the <activity>
element. The following example enables hardware acceleration for the entire application but disables it for one activity:
<application android:hardwareAccelerated="true"> <activity ... /> <activity android:hardwareAccelerated="false" /> </application>
Window level
If you need even more fine-grained control, you can enable hardware acceleration for a given window with the following code:
getWindow().setFlags( WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Note: You currently cannot disable hardware acceleration atthe window level.
View level
You can disable hardware acceleration for an individual view at runtime with thefollowing code:
myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Note: You currently cannot enable hardware acceleration atthe view level. View layers have other functions besides disabling hardware acceleration. See View layers for more information about their uses.
Determining if a View is Hardware Accelerated
It is sometimes useful for an application to know whether it is currently hardware accelerated, especially for things such as custom views. This is particularly useful if your application does a lot of custom drawing and not all operations are properly supported by the new rendering pipeline.
There are two different ways to check whether the application is hardware accelerated:
View.isHardwareAccelerated()
returnstrue
if theView
is att