At the time of drawing, if the picture is rotated or scaled, there will always be those gorgeous jagged edges. In fact, Android comes with a solution.
Method One: Add an anti-aliasing flag to paint. The Paint object is then passed as a parameter to the canvas's drawing method.
Java code
- Paint.setantialias (true);
Method Two: Add anti-aliasing flags to the canvas.
Some places can not use paint, directly to the canvas anti-aliasing, more convenient.
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 ( 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);
- }
- }
is the effect:
As you can see, both of these methods are quite effective.
Anti-aliasing of Android graphics