需要注意的方法如下:
- Canvas
- Paint
- Xfermodes
In addition, some operations behave differently with hardware acceleration enabled:
- Canvas
clipRect()
:XOR
,Difference
andReverseDifference
clip modes are ignored. 3D transforms do not apply to the clip rectangledrawBitmapMesh()
: colors array is ignored
- Paint
setDither()
: ignoredsetFilterBitmap()
: filtering is always onsetShadowLayer()
: works with text only
- PorterDuffXfermode
PorterDuff.Mode.DARKEN
will be equivalent toSRC_OVER
when blending against the framebuffer.PorterDuff.Mode.LIGHTEN
will be equivalent toSRC_OVER
when blending against the framebuffer.PorterDuff.Mode.OVERLAY
will be equivalent toSRC_OVER
when blending against the framebuffer.
- ComposeShader
ComposeShader
can only contain shaders of different types (aBitmapShader
and aLinearGradient
for instance, but not two instances ofBitmapShader
)ComposeShader
cannot contain aComposeShader
如在Canvas.clipPath时会抛出异常
UnsupportedOperationException in GLES20Canvas.clipPath with hardware acceleration disabled on view
解决办法:在程序中通过反射禁掉硬件加速
private static Method getMethod(Class<?> clazz, String name, Class<?>... parameterTypes) {
try {
return clazz.getMethod(name, parameterTypes);
}
catch (NoSuchMethodException e) {
return null;
}
}
public static void disableHardwareAcceleration(View view) {
try {
Method setLayerTypeMethod = getMethod(View.class, "setLayerType", int.class,
Paint.class);
if (setLayerTypeMethod != null) {
int layerType = 1; // View.LAYER_TYPE_SOFTWARE
setLayerTypeMethod.invoke(view, layerType, null);
}
}
catch (Exception ignored) {
}
}