在Canvas上绘制位图时的质量很差

时间:2015-02-12 03:02:48

标签: android canvas bitmap

我正在尝试使用此代码绘制头像位图。但是位图是像素化的。这是我的代码。目前我使用createScaledBitmap来调整头像图像的大小。在某些具有高分辨率的设备上,Text也稍微小一点

BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inMutable = true;

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.card_nodpi, opt) ;

Canvas canvas = new Canvas(bitmap);

   Paint paint = new Paint();
   paint.setColor(Color.BLACK); 
   paint.setTextSize(40);
   paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
   canvas.drawText("The quick brown fox", x, y, paint);

   Paint paint2 = new Paint();
   paint2.setColor(Color.BLACK);
   paint2.setTextSize(30);


   canvas.drawText("The quick brown fox", x, y + (40), paint2);
   canvas.drawText("The quick brown fox", x, y + ((40 * 2)), paint2);

   if (avatar != null) {
      Bitmap img = Bitmap.createScaledBitmap(avatar, 250, 250, false);
      canvas.drawBitmap(img, bitmap.getWidth() - img.getWidth() - x, y - 40, new Paint(Paint.FILTER_BITMAP_FLAG));
  }

 imageView.setImageBitmap(bitmap);

1 个答案:

答案 0 :(得分:1)

createScaledBitmap可以产生一些时髦/质量差的图像。 在这里尝试这个解决方案: https://stackoverflow.com/a/7468636/4557530

请告诉我这是否适合您!

可替换地, 试试这个,我之前使用过这段代码,感谢一些我不再记得的博客

  Bitmap newBM = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

  float scaleX = newWidth / (float) origBM.getWidth();
  float scaleY = newHeight / (float) origBM.getHeight();
  float pivX = 0;
  float pivY = 0;

  Matrix scaleMatrix = new Matrix();
  scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

  Canvas canvas = new Canvas(newBM);
  canvas.setMatrix(scaleMatrix);
  canvas.drawBitmap(origBM, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG));