1

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

1 Answers1

0

createScaledBitmap can produce some funky/bad quality images. Try out this solution here: 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));
Community
  • 1
  • 1