简体   繁体   English

在画布上绘制位图时的质量差

[英]BAD quality when drawing a bitmap on a Canvas

I am trying to draw a avatar bitmap using this code. 我正在尝试使用此代码绘制头像位图。 But the bitmap is pixelated. 但是位图是像素化的。 Here is my Code. 这是我的代码。 Currently I use createScaledBitmap to resize the avatar image. 目前,我使用createScaledBitmap调整头像图像的大小。 Also the Text are a little smaller on some devices with a high resolution 此外,在某些具有高分辨率的设备上,“文本”要小一些

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

createScaledBitmap can produce some funky/bad quality images. createScaledBitmap可能会生成一些时髦/质量较差的图像。 Try out this solution here: https://stackoverflow.com/a/7468636/4557530 在此处尝试此解决方案: https : //stackoverflow.com/a/7468636/4557530

Let me know if that does anything for you! 让我知道这是否对您有帮助!

Alternatively, try this, I used this code before thanks to some blog, which I don't remember anymore 另外,尝试一下,在感谢一些博客之前,我已经使用了此代码,我已经不记得了

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM