栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android画图之抗锯齿paint和Canvas两种方式实例

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android画图之抗锯齿paint和Canvas两种方式实例

在画图的时候,图片如果旋转或缩放之后,总是会出现那些华丽的锯齿。其实Android自带了解决方式。

方法一:给Paint加上抗锯齿标志。然后将Paint对象作为参数传给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); 
  } 
} 

下图是效果:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158996.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号