2

I want to load a ImageView with a png file with low resolution. I want to display the pixels but the ImageView is displaying it blurry with interpolation. How can this be avoided? I want to display all the pixels.

This is the way I'm displaying the ImageView:

Drawable dr = getResources().getDrawable(R.drawable.cloud);
Bitmap cloud1Bitmap = ((BitmapDrawable) dr).getBitmap();
float cloud1Ratio = (float)cloud1Bitmap.getHeight()/cloud1Bitmap.getWidth();

cloud1ImageView = new ImageView(this);
cloud1ImageView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
cloud1ImageView.setImageBitmap(Bitmap.createScaledBitmap(cloud1Bitmap, cloud1Width, (int) (cloud1Width*cloud1Ratio), true));
main.addView(cloud1ImageView);

EDIT:

Tryed with that function and didn't work. Maybe it's because I'm scaling the image?

    view.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    width = (int) (sw/3);
    Drawable dr = context.getResources().getDrawable(R.drawable.ufo);
    BitmapDrawable bd = ((BitmapDrawable) dr);
    Bitmap bitmap = bd.getBitmap();
    float ratio = (float)bitmap.getHeight()/bitmap.getWidth();
    height = (int)(width*ratio);

    BitmapDrawable drawable = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, width, height, false));
    drawable.setFilterBitmap(false);        
    view.setImageDrawable(drawable);

In the left you can see how the image appears on the game and in the right how is in the original png:

enter image description here

NullPointerException
  • 36,107
  • 79
  • 222
  • 382

1 Answers1

2

As @pskink says, you can stop the blurring/interpolation by calling setFilterBitmap(false). For these examples, I'm using a 24px image in a 240dp ImageView:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_android);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);

ImageView image = findViewById(R.id.image);
image.setImageDrawable(drawable);

enter image description here

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_android);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
drawable.setFilterBitmap(false);

ImageView image = findViewById(R.id.image);
image.setImageDrawable(drawable);

enter image description here

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • but what happens if you need to scale the image to a desired size like I need. You can see it in my sample code. I tryed adding that function and nothing happens. Maybe is because of the scale? – NullPointerException Oct 02 '18 at 07:02