Android Bitmap Masking (Xfermode) leaves opaque black background behind

前端 未结 1 1670
日久生厌
日久生厌 2020-12-14 22:52

I have a custom view and in the onDraw(), I am trying to perform bitmap masking. I have a squareBitmap (red color) which fills the whole view, and

相关标签:
1条回答
  • 2020-12-14 23:39

    It seems that to get the Xfermode done, I need to redirect the drawing to an offscreen bitmap. So adding the following fixed my problem.

    canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), q);
    

    But then again, the documentation of saveLayer says to avoid using this method since it is expensive. It is suggested to use a harware layer instead of this method.

    Avoid using this method, especially if the bounds provided are large, or if the CLIP_TO_LAYER_SAVE_FLAG is omitted from the saveFlags parameter. It is recommended to use a hardware layer on a View to apply an xfermode, color filter, or alpha, as it will perform much better than this method.

    Hence, intead of saveLayer method, I was able to fix it by adding the following:

    setLayerType(LAYER_TYPE_HARDWARE, q);
    
    0 讨论(0)
提交回复
热议问题