Android画图之抗锯齿
by
at 2010-10-26 15:25:05
original http://www.javaeye.com/topic/794505
在画图的时候,图片如果旋转或缩放之后,总是会出现那些华丽的锯齿。其实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); } }
下图是效果:
可以看出,两种方式都挺有效的。
<br><br>
作者: <a href="http://chroya.javaeye.com">chroya</a>
<br>
声明: 本文系JavaEye网站发布的原创文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
<br><br>
<span style="color:red">
<a href="http://www.javaeye.com/topic/794505" style="color:red">已有 <strong>0</strong> 人发表回复,猛击->><strong>这里</strong><<-参与讨论</a>
</span>
<br><br><br>
JavaEye推荐