Anti-aliasing of Android image painting and android image painting
When you draw a picture, if the image is rotated or scaled, there will always be those gorgeous sawtooth. In fact, Android comes with a solution.
Method 1: add the anti-tooth mark to the Paint. The painting object is passed as a parameter to the painting method of canvas.
Java code
- Paint. setAntiAlias (true );
Method 2: add the anti-tooth mark to the Canvas.
In some cases, painting is not used, so it is more convenient to directly add anti-aliasing to the canvas.
Java code
- Canvas. setDrawFilter (new PaintFlagsDrawFilter (0, Paint. ANTI_ALIAS_FLAG | Paint. FILTER_BITMAP_FLAG ));
The test code is as follows:
Java code
- Import android. content. Context;
- Import android. graphics. Bitmap;
- Import android. graphics. BitmapFactory;
- Import android. graphics. Canvas;
- Import android. graphics. Matrix;
- Import android. graphics. Paint;
- Import android. graphics. PaintFlagsDrawFilter;
- Import android. view. View;
- Public class MyView extends View {
- Private PaintFlagsDrawFilter pfd;
- Private Paint mPaint = new Paint ();
- Private Matrix matrix = new Matrix ();;
- Private Bitmap bmp;
- Public MyView (Context context ){
- Super (context );
- Initialize ();
- }
- Private void initialize (){
- Pfd = new PaintFlagsDrawFilter (0, Paint. ANTI_ALIAS_FLAG | Paint. FILTER_BITMAP_FLAG );
- MPaint. setAntiAlias (true );
- Matrix. setRotate (30 );
- Matrix. postScale (0.5f, 0.5f );
- Bmp = BitmapFactory. decodeResource (getResources (), R. drawable. show );
- }
- @ Override
- Public void dispatchDraw (Canvas canvas ){
- Canvas. translate (100, 0 );
- Canvas. drawBitmap (bmp, matrix, null );
- Canvas. translate (0,250 );
- Canvas. drawBitmap (bmp, matrix, mPaint );
- Canvas. setDrawFilter (pfd );
- Canvas. translate (0,250 );
- Canvas. drawBitmap (bmp, matrix, null );
- }
- }
Result:
We can see that both methods are quite effective.
Android image profiling
Difficult
Who can help solve this problem?
Replace the flag in the phrase "paint = new Paint (Paint. FILTER_BITMAP_FLAG)" with "ANTI_ALIAS_FLAG.