从android3.0开始,2D渲染开始支持硬件加速,即在view的Canvas上的绘图操作可以用GPU来加速。
硬件加速会使app消耗更多的内存。
如果配置文件中,Target API level >=14 硬件加速是默认开启的。
如果应用只是使用了标准的 view 和 drawable ,那么对app开启全局的硬件加速不会有什么问题。 但由于硬件加速并非支持所有的2d绘图操作,所以对使用了自定义的view和drawable,可能会产生不利的影响。比如view不见了,异常,或者渲染不正确等。要在实际的设备上测试。
可在这几个级别控制是否支持硬件加速:
Application
Activity
Window
View
1. 在Applciation级别控制硬件加速:
<application android:hardwareAccelerated="true" ...>
2. Activity级别 对单个的Activity控制是否启用硬件加速:
下面的例子在整个应用中启用了硬加速但对某个activity禁止了硬加速:
<application android:hardwareAccelerated="true">
<activity ... />
<activity android:hardwareAccelerated="false" />
</application>
3. Window级别(Added in API level 11) (现在对window级别只能开启不能禁用硬件加速)
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
4. View级别 可以在View级别禁用硬件加速。(注:当前不能在View级别启用硬加速.)
myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); (Added in API level 11)
或者使用android:layerType="software"来关闭硬件加速:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="2dp"
android:layerType="software"
android:paddingRight="2dp" >
1)LAYER_TYPE_NONE: The view is rendered normally and is not backed by an off-screen buffer. This is the default behavior.
2)LAYER_TYPE_HARDWARE: The view is rendered in hardware into a hardware texture if the application is hardware accelerated. If the application is not hardware accelerated, this layer type behaves the same as LAYER_TYPE_SOFTWARE.
3)LAYER_TYPE_SOFTWARE: The view is rendered in software into a bitmap. The type of layer you use depends on your goal: Performance: Use a hardware layer type to render a view into a hardware texture. Once a view is rendered into a layer, its drawing code does not have to be executed until the view calls invalidate(). Some animations, such as alpha animations, can then be applied directly onto the layer, which is very efficient for the GPU to do.
Visual effects: Use a hardware or software layer type and a Paint to apply special visual treatments to a view. For instance, you can draw a view in black and white using a ColorMatrixColorFilter.
Compatibility: Use a software layer type to force a view to be rendered in software. If a view that is hardware accelerated (for instance, if your whole application is hardware acclerated), is having rendering problems, this is an easy way to work around limitations of the hardware rendering pipeline.
如何判断一个View是否启用了硬件加速
View.isHardwareAccelerated() returns true if the View is attached to a hardware accelerated window. Canvas.isHardwareAccelerated() returns true if the Canvas is hardware accelerated
如果必须在你的绘制代码中做这个判断,应使用Canvas.isHardwareAccelerated() 而不是View.isHardwareAccelerated(). 因为当一个view附加到一个硬加速的window上,它仍可以使用非硬件速的Canvas进行绘制操作.比如当为了高速缓存而把一个view画到一个bitmap中.
硬件加速可以提高View的动画效率,避免在动画的过程中,不停的重绘自己。 View只有在手动调用invalidate(),或者调用了改变属性的方法,而这个方法又导致了invalidate()的调用时,才会重绘。
并非所有的2D绘图操作都支持硬件加速:
1.名词解释
GPU:Graphic Processing Unit (图形处理器)
OpenGL:Open Graphic Library 定义了一个跨编程语言、跨平台的编程接口的规格,不同厂商会有不同的实现方法,它主要用于三维图象(二维的亦可)绘制。
SurfaceFlinger:Android中负责Surface之间叠加、混合操作的动态库
Skia:Android中的2D图形库
libagl:Android中通过软件方法实现的一套OpenGL动态库
libhgl:为区别libagl,自定义的一种叫法。特指GPU厂商提供的硬件实现的OpenGL
composition:特指SurfaceFlinger对各个Surface之间的叠加、混合操作
render:特指使用OpenGL动态库进行3D渲染
copybit:Android使用2D引擎来加速图形操作(主要是Surface之间的composition操作)的一种技术,对应着一个或几个动态库。
pmem:Android特有驱动,从linux内核中reserve物理连续内存,可以为2d、3d引擎、vpu等设备分配物理连续内存。