Android畫圖之消除鋸齒

來源:互聯網
上載者:User

標籤:android   blog   http   io   os   ar   java   sp   div   

 在畫圖的時候,圖片如果旋轉或縮放之後,總是會出現那些華麗的鋸齒。其實Android內建瞭解決方式。
    方法一:給Paint加上消除鋸齒標誌。然後將Paint對象作為參數傳給canvas的繪製方法。

 Java代碼  
  1. paint.setAntiAlias(true);  

 


    方法二:給Canvas加上消除鋸齒標誌。
有些地方不能用paint的,就直接給canvas加消除鋸齒,更方便。

 Java代碼  
  1. canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));  

 

      測試代碼如下: 

Java代碼  
  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.   
  10. public class MyView extends View {  
  11.     private PaintFlagsDrawFilter pfd;  
  12.     private Paint mPaint = new Paint();  
  13.     private Matrix matrix = new Matrix();;  
  14.     private Bitmap bmp;  
  15.   
  16.     public MyView(Context context) {  
  17.         super(context);  
  18.         initialize();  
  19.     }  
  20.   
  21.     private void initialize() {  
  22.         pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);        
  23.         mPaint.setAntiAlias(true);  
  24.         matrix.setRotate(30);  
  25.         matrix.postScale(0.5f, 0.5f);  
  26.         bmp = BitmapFactory.decodeResource(getResources(), R.drawable.show);  
  27.     }  
  28.       
  29.     @Override  
  30.     public void dispatchDraw(Canvas canvas) {  
  31.         canvas.translate(100, 0);  
  32.         canvas.drawBitmap(bmp, matrix, null);  
  33.         canvas.translate(0, 250);  
  34.         canvas.drawBitmap(bmp, matrix, mPaint);  
  35.         canvas.setDrawFilter(pfd);  
  36.         canvas.translate(0, 250);  
  37.         canvas.drawBitmap(bmp, matrix, null);  
  38.     }  
  39. }  

 

    是效果:

 

      可以看出,兩種方式都挺有效。

Android畫圖之消除鋸齒

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.