In the process of reading the program source code today, I came across a piece of code like this:
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG |Paint.FILTER_BITMAP_FLAG));
I don’t quite understand , Google took a look, it turned out to be: anti-aliasing processing. At the same time, I learned about two anti-aliasing methods in Android drawing.
First of all, we must understand what is the sawtooth? As shown in the figure below, the left side is the graphics without anti-aliasing treatment, and the right side is the graphics after anti-aliasing treatment. It is obvious that the graphics after anti-aliasing treatment will appear smoother.
On Android This problem also occurs in drawing, but why is there jaggedness?
①When we use Canvas to draw a bitmap, if the bitmap is selected, the bitmap will appear jagged.
② When using View’s RotateAnimation to animate, if the View contains a large number of graphics, jaggedness will also appear.
So how to solve it? As long as you grasp one point, if you can set it through Paint, use Paint to set it. If you can’t use Paint to set it, add anti-aliasing to Canvas directly.
Solution one, set it through Paint:
First of all, in your In the constructor, a Paint needs to be created.
Paint mPaint = new Paint();
Then, you need to set two parameters:
1 ) mPaint.setAntiAlias();
Solution 2: Set via Canvas
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI _ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
In short, to solve the problem of sawtooth:
1. Set by Paint
2. Set by Canvas