您的位置:首页 > 其它

开启硬件加速之后,许多2D的绘制方法会抛出异常

2013-06-06 11:02 363 查看
需要注意的方法如下:

 

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
LinearGradient
 for instance, but not two instances of 
BitmapShader
 )
ComposeShader
 cannot contain a 
ComposeShader


如在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) {

                }

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐