public synchronized static Bitmap scaleBitmap(Bitmap bitmap, float w, float h,boolean isAutoFree) {
if (bitmap == null || bitmap.isRecycled()) {
return null;
}
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scaleW = w / (float) width;
float scaleH = h / (float) height;
float scale = Math.max(scaleH,scaleW);
if(scale > 1F){
return bitmap;
}
Matrix matrix = new Matrix();
matrix.postScale(scale, scale,width/2,height/2);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
if(isAutoFree){
bitmap.recycle();
}
return scaledBitmap;
}
在文章 Android画图之抗锯齿 中找到解决方案,采用方案二给canvas设置抗锯齿工具的方式:
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
ps:使用抗锯齿之后,在低端机似乎绘制效率变低明显。