Anti-aliasing of Android graphics

Source: Internet
Author: User

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
    1. 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
    1. Canvas.setdrawfilter (new Paintflagsdrawfilter (0, Paint.anti_alias_flag|  Paint.filter_bitmap_flag));

The test code is as follows:

Java code
  1. Import Android.content.Context;
  2. Import Android.graphics.Bitmap;
  3. Import Android.graphics.BitmapFactory;
  4. Import Android.graphics.Canvas;
  5. Import Android.graphics.Matrix;
  6. Import Android.graphics.Paint;
  7. Import Android.graphics.PaintFlagsDrawFilter;
  8. Import Android.view.View;
  9. Public class MyView extends View {
  10. private Paintflagsdrawfilter PFD;
  11. private Paint Mpaint = new Paint ();
  12. private Matrix matrix = new Matrix ();
  13. private Bitmap bmp;
  14. Public MyView (context context) {
  15. super (context);
  16. Initialize ();
  17. }
  18. private Void Initialize () {
  19. PFD = New Paintflagsdrawfilter (0, Paint.anti_alias_flag|        Paint.filter_bitmap_flag);
  20. Mpaint.setantialias (true);
  21. Matrix.setrotate (30);
  22. Matrix.postscale (0.5f, 0.5f);
  23. BMP = Bitmapfactory.decoderesource (Getresources (), r.drawable.show);
  24. }
  25. @Override
  26. public void Dispatchdraw (canvas canvas) {
  27. Canvas.translate ( 0);
  28. Canvas.drawbitmap (BMP, Matrix, null);
  29. Canvas.translate (0, 250);
  30. Canvas.drawbitmap (BMP, Matrix, mpaint);
  31. Canvas.setdrawfilter (PFD);
  32. Canvas.translate (0, 250);
  33. Canvas.drawbitmap (BMP, Matrix, null);
  34. }
  35. }

is the effect:

As you can see, both of these methods are quite effective.

Anti-aliasing of Android graphics

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.