【吐槽】我与Path的恩怨情仇

       最近项目中遇到了很多需要用Path切割画布的效果,但是我是怎么切,都是切不出来。但是用Canvas  drawPath效果和预想的效果是一致,但是就是执行clipPath,出来的效果完全不是自己想要的效果。今天终于发现了原因。

      

       硬件加速的问题

       好多手机上一旦开启硬件加速,还会报以下的问题:

      java.lang.unsupported operation exception, android.view.GLES20Canvas.clipPath(GLES20Canvas...

    

  所以一旦出现上述异常,那么我们就应该立即想到,是硬件加速出了问题。

  开启硬件加速后,以下方法都会出现异常:

   Canvas clipPath() clipRegion() drawPicture() drawTextOnPath() drawVertices() Paint setLinearText() setMaskFilter() setRasterizer() XfermodesAvoidXfermode 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  


  

    要解决以上问题,那么就需要关闭硬件加速。如何处理硬件加速的开关状态呢?

   硬件加速会在四个层进行操作
    1.Application

       <applicationandroid:hardwareAccelerated="true" ...>

    2.Activity

       <application android:hardwareAccelerated="true"> <activity ... /> <activityandroid:hardwareAccelerated="false" /> </application>

    3.Window

getWindow().setFlags( WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

    4.View

   myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);


如何判断是否开启了硬件加速

在绘制之前,你必须做该测试 Canvas.isHardwareAccelerated() 替代View.isHardwareAccelerated() 在必要时。

View.isHardwareAccelerated() ; // returns true if the View is attached to a hardware accelerated window

.Canvas.isHardwareAccelerated();// returns true if the Canvas is hardware accelerated




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值