Anti-aliasing solution for Android programming drawing _android

Source: Internet
Author: User

An example of this paper is to analyze the anti aliasing method of Android programming drawing. Share to everyone for your reference, specific as follows:

When drawing, the picture, if rotated or scaled, will always appear in those gorgeous jagged. In fact, Android has its own way of dealing with it.

Method One: Add anti-aliasing mark to paint. The Paint object is then passed as an argument to the canvas drawing method.

Copy Code code as follows:
Paint.setantialias (TRUE);

Method Two: To add anti-aliasing mark to canvas.

Some places can not use paint, directly to canvas add anti-aliasing, more convenient.

Copy Code code as follows:
Canvas.setdrawfilter (New Paintflagsdrawfilter (0, Paint.anti_alias_flag| Paint.filter_bitmap_flag));

The test code is as follows:

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 {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);

 }
}

The following illustration is an effect:

As can be seen, both methods are quite effective.

I hope this article will help you with the Android program.

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.