论坛首页 移动开发技术论坛

Android画图之抗锯齿

浏览 25954 次
精华帖 (5) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-10-26   最后修改:2010-10-26

    在画图的时候,图片如果旋转或缩放之后,总是会出现那些华丽的锯齿。其实Android自带了解决方式。
    方法一:给Paint加上抗锯齿标志。然后将Paint对象作为参数传给canvas的绘制方法。

paint.setAntiAlias(true);

 


    方法二:给Canvas加上抗锯齿标志。
有些地方不能用paint的,就直接给canvas加抗锯齿,更方便。

canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));

 

      测试代码如下:

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);
	}
}
 

    下图是效果:

 

      可以看出,两种方式都挺有效的。

   发表时间:2010-10-29  
像这种有用的效果,为什么android不默认设定这个标记?
是因为抗锯齿会消耗更多的计算?
还是有其它考虑?
0 请登录后投票
   发表时间:2010-10-29  
毕竟红尘 写道
像这种有用的效果,为什么android不默认设定这个标记?
是因为抗锯齿会消耗更多的计算?
还是有其它考虑?

抗锯齿除了更多计算,还占用更多显存,在android上就是消耗更多内存了
0 请登录后投票
   发表时间:2010-10-29  
再有就是很多人喜欢的就是锯齿 ~~ 突然没有了肯定会大喊大叫了~~
0 请登录后投票
   发表时间:2010-11-01  
谢谢,按你的方法我消除了图片的指针的锯齿.
附上效果图:
自己画的,比较丑.
  • 大小: 17.2 KB
0 请登录后投票
   发表时间:2010-11-29  
paint加抗锯齿和Canvas加抗锯齿效果是一样的。
0 请登录后投票
   发表时间:2010-12-09  
看不出什么区别
0 请登录后投票
   发表时间:2011-01-11  
嗯,这是个方法!
NinePatch
0 请登录后投票
   发表时间:2011-01-11  
canvas的方法的效果要好一些。
0 请登录后投票
   发表时间:2011-07-10  
非常不错的方法,感谢分享。
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics