Android畫圖之抗鋸齒paint和Canvas兩種方式實例
更新時間:2017年04月13日 15:33:26 作者:安兒IT
本篇文章主要介紹了Android畫圖之抗鋸齒paint和Canvas兩種方式實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
在畫圖的時候,圖片如果旋轉(zhuǎn)或縮放之后,總是會出現(xiàn)那些華麗的鋸齒。其實Android自帶了解決方式。
方法一:給Paint加上抗鋸齒標志。然后將Paint對象作為參數(shù)傳給canvas的繪制方法。
paint.setAntiAlias(true);
方法二:給Canvas加上抗鋸齒標志。
有些地方不能用paint的,就直接給canvas加抗鋸齒,更方便。
復制代碼 代碼如下:
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
eg:
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); } }
下圖是效果:
可以看出,兩種方式都挺有效的。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android開發(fā)手冊自定義Switch開關按鈕控件
這篇文章主要為大家介紹了Android開發(fā)手冊自定義Switch開關按鈕控件的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06Android自定義View 仿QQ側(cè)滑菜單的實現(xiàn)代碼
這篇文章主要介紹了Android自定義View 仿QQ側(cè)滑菜單的實現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-08-08Android手機開發(fā) 控件 TextView文字居中
本文主要介紹Android手機開發(fā)TextView居中的方法,希望能幫到大家。2016-05-05Android ScrollView滑動實現(xiàn)仿QQ空間標題欄漸變
這篇文章主要為大家詳細介紹了Android ScrollView滑動實現(xiàn)仿QQ空間標題欄漸變,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08