Anti-aliasing in Android

Source: Internet
Author: User

Currently, in Android, I know that there are two kinds of sawtooth situations.

1) when we use a canvas to draw a bitmap, If we select a bitmap, the bitmap will appear at the same time.

2) When you use the rotateanimation of a view for animation, if the view contains a large number of images, the page will also be distorted.

We will consider these two situations separately.

1. Use canvas to draw a bit.

When using canvas to draw a bitmap, we generally use the drawbitmap function family. In these functions, there is a paint parameter. To prevent aliasing, we need to use this parameter.

First, you need to create a paint in your constructor.

Paint mpaint = new paint ()

Then, you need to set two parameters:

1) mpaint. setantialias ();

2) mpaint. setbitmapfilter (true ).

The first function is used to prevent the Sawtooth of the edge, and the second function is used to filter the bitmap. Finally, when drawing, call the drawbitmap function, you only need to pass the entire painting.

2. rotateanimation

Sometimes, when you create a rotateanimation, you will find that the nasty sawtooth appears again. At this time, because you cannot control the plotting of bitmap, you can only use other methods to prevent the aliasing. In addition, if you draw many bitmaps. You do not want to input a painting for each bitmap. In other cases, you cannot control the rendering of each window. You need to use the following method to process the entire canvas.

1) create a paint filter in your constructor.

Paintflagsdrawfilter msetfil = new paintflagsdrawfilter (0, paint. filter_bitmap_flag );

The first parameter is the flag you want to clear, and the second parameter is the flag you want to set. Filter bitmap.

2) When you are drawing a picture, if it is a view, it is in ondraw. If it is a viewgroup, the following function is called in dispatchdraw.

Canvas. setdrawfilter (msetfil );

3. drawable

Finally, in the drawable class and its sub-classes, the setfilterbitmap function can be used to filter bitmap. In this way, when you select drawable, it will have the anti-aliasing effect.

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.