Home>

When drawing,If the picture is rotated or scaled,Those gorgeous sawtooth always appear.In fact, android comes with a solution.

Method 1:Add anti-aliasing to the paint.Then pass the paint object as a parameter to the drawing method of the canvas.

paint.setantialias (true);

Method 2:Add anti-aliasing to the canvas.

In some places, you can't use paint, just add anti-aliasing to the canvas.more convenient.

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 (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 (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 figure is the effect:

As can be seen,Both methods are quite effective.

  • Previous Summary of methods for creating objects in js
  • Next Summary of jQuery data types (14)