在畫圖的時候,圖片如果旋轉或縮放之後,總是會出現那些華麗的鋸齒。其實android自帶了解決方式。
方法一:給Paint加上抗鋸齒標志。然後將Paint對象作為參數傳給canvas的繪制方法。
Java代碼
- paint.setAntiAlias(true);
- paint.setAntiAlias(true);
方法二:給Canvas加上抗鋸齒標志。
有些地方不能用paint的,就直接給canvas加抗鋸齒,更方便。
Java代碼
- canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
- canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
測試代碼如下:
Java代碼
- 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);
- }
- }