Android 自定义View 抗锯齿

1.设置paint抗锯齿

            val bgPaint = Paint().apply {
                isAntiAlias = true  //抗锯齿
                isDither = true      //防抖动
                isFilterBitmap = true    //bitmap抗锯齿
            }

2.设置canvas抗锯齿

在
override fun onDraw(canvas: Canvas) {}
或者
override fun dispatchDraw(canvas: Canvas) {}
中调用    canvas.drawFilter = paintFlagsDrawFilter


            val paintFlagsDrawFilter = PaintFlagsDrawFilter(
                0,
                Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG
            )

            canvas.drawFilter = paintFlagsDrawFilter

3.关闭硬件加速

在View中调用

 setLayerType(View.LAYER_TYPE_SOFTWARE, null)  //关闭硬件加速

附上开启的代码 myView.setLayerType(View.LAYER_TYPE_HARDWARE, null); //开启硬件加速

你可能感兴趣的:(Android 自定义View 抗锯齿)