Anti-aliasing of Android image painting and android image painting

Source: Internet
Author: User

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  
  1. 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  
  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 (100, 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. }

 

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.

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.