Skip to content

Instantly share code, notes, and snippets.

@z8888q
Created October 25, 2012 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z8888q/3950635 to your computer and use it in GitHub Desktop.
Save z8888q/3950635 to your computer and use it in GitHub Desktop.
Android Control Hardware Acceleration (硬件加速)
http://stackoverflow.com/questions/8895677/work-around-canvas-clippath-that-is-not-supported-in-android-any-more
http://developer.android.com/guide/topics/graphics/hardware-accel.html
硬件加速不支持的操作
When hardware accelerated, the 2D rendering pipeline supports the most commonly used Canvas drawing operations as well as many less-used operations. All of the drawing operations that are used to render applications that ship with Android, default widgets and layouts, and common advanced visual effects such as reflections and tiled textures are supported. The following list describes known operations that are not supported with hardware acceleration:
Canvas
clipPath()
clipRegion()
drawPicture()
drawTextOnPath()
drawVertices()
Paint
setLinearText()
setMaskFilter()
setRasterizer()
Xfermodes
AvoidXfermode
PixelXorXfermode
In addition, some operations behave differently with hardware acceleration enabled:
Canvas
clipRect(): XOR, Difference and ReverseDifference clip modes are ignored. 3D transforms do not apply to the clip rectangle
drawBitmapMesh(): colors array is ignored
Paint
setDither(): ignored
setFilterBitmap(): filtering is always on
setShadowLayer(): works with text only
PorterDuffXfermode
PorterDuff.Mode.DARKEN will be equivalent to SRC_OVER when blending against the framebuffer.
PorterDuff.Mode.LIGHTEN will be equivalent to SRC_OVER when blending against the framebuffer.
PorterDuff.Mode.OVERLAY will be equivalent to SRC_OVER when blending against the framebuffer.
ComposeShader
ComposeShader can only contain shaders of different types (a BitmapShader and a LinearGradient for instance, but not two instances of BitmapShader )
ComposeShader cannot contain a ComposeShader
If your application is affected by any of these missing features or limitations, you can turn off hardware acceleration for just the affected portion of your application by calling setLayerType(View.LAYER_TYPE_SOFTWARE, null). This way, you can still take advantage of hardware acceleratin everywhere else. See Controlling Hardware Acceleration for more information on how to enable and disable hardware acceleration at different levels in your application.
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 at the window level.
View level
You can disable hardware acceleration for an individual view at runtime with the following code:
myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Note: You currently cannot enable hardware acceleration at the view level. View layers have other functions besides disabling hardware acceleration. See View layers for more information about their uses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment