Bitmap not drawn anti-aliased

后端 未结 5 1172
遥遥无期
遥遥无期 2020-12-05 00:50

I have a custom View that always draws a Bitmap at a certain rotation. I overwrite the onDraw method, rotate the Canvas a

5条回答
  •  爱一瞬间的悲伤
    2020-12-05 01:26

    I have found why I did not get any anti-aliasing: hardware acceleration. After I (by pure accident) tested on my Nexus One it suddenly worked, while my Nexus 7 didn't. Turns out that the ANTI_ALIAS_FLAG does nothing when the Canvas is drawn using the hardware acceleration. With that insight I found Romain Guy's answer on StackOverflow which answers my question.

    Solutions seem to be: adding a transparent border to every bitmap (manually or on the fly) or 'do something with shaders'. Neither are very appealing to me. Instead I disabled hardware acceleration on my custom view using the setLayerType method. Note that this is available since API level 11, but (not by accident) you won't have to deal with hardware acceleration before any way. So I have added, in the view constructor:

        if (android.os.Build.VERSION.SDK_INT >= 11) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    

    I've updated the above referenced open-source project on BitBucket for those interested.

提交回复
热议问题