title: How to downsample images correctly?
body: <h2>Background</h2>
  
  <p>Creating an app that has a lot of high quality images, I've decided to downscale the images to the needed size (meaning that if the image is larger than the screen , I downscale it ) .</p>
  
  <h2>The problem</h2>
  
  <p>I've noticed that on some devices, if the images are downscaled, they become blurry/pixelated, yet on the same devices, for the same target imageView size, if the images aren't downscaled, they look just fine.</p>
  
  <h2>What I've tried</h2>
  
  <p>I've decided to check this issue further, and created a small POC app that shows the issue.</p>
  
  <p>Before showing you code, here's a demo of what I'm talking about :</p>
  
  <p><img src="https://i.stack.imgur.com/Mdtok.png" alt="enter image description here"></p>
  
  <p>it's a bit hard to see the difference, but you can see that the second is a bit pixelated . this can be shown on any image. </p>
  
  <pre><code>public class MainActivity extends Activity
    {
    @Override
    protected void onCreate(final Bundle savedInstanceState)
      {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final ImageView originalImageView=(ImageView)findViewById(R.id.originalImageView);
      final ImageView halvedImageView=(ImageView)findViewById(R.id.halvedImageView);
      final ImageView halvedBitmapImageView=(ImageView)findViewById(R.id.halvedBitmapImageView);
      //
      final Bitmap originalBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.test);
      originalImageView.setImageBitmap(originalBitmap);
      halvedImageView.setImageBitmap(originalBitmap);
      //
      final LayoutParams layoutParams=halvedImageView.getLayoutParams();
      layoutParams.width=originalBitmap.getWidth()/2;
      layoutParams.height=originalBitmap.getHeight()/2;
      halvedImageView.setLayoutParams(layoutParams);
      //
      final Options options=new Options();
      options.inSampleSize=2;
      // options.inDither=true; //didn't help
      // options.inPreferQualityOverSpeed=true; //didn't help
      final Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.test,options);
      halvedBitmapImageView.setImageBitmap(bitmap);
      }
    }
  </code></pre>
  
  <p>xml:</p>
  
  <pre><code>&lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".MainActivity"
    android:fillViewport="true"&gt;
    &lt;HorizontalScrollView android:layout_width="match_parent"
      android:fillViewport="true" android:layout_height="match_parent"&gt;
      &lt;LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent" android:orientation="vertical"&gt;
  
  
        &lt;TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:text="original" /&gt;
  
        &lt;ImageView android:layout_width="wrap_content"
          android:id="@+id/originalImageView" android:layout_height="wrap_content" /&gt;
  
        &lt;TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:text="original , imageView size is halved" /&gt;
  
        &lt;ImageView android:layout_width="wrap_content"
          android:id="@+id/halvedImageView" android:layout_height="wrap_content" /&gt;
  
        &lt;TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:text="bitmap size is halved" /&gt;
  
        &lt;ImageView android:layout_width="wrap_content"
          android:id="@+id/halvedBitmapImageView" android:layout_height="wrap_content" /&gt;
  
      &lt;/LinearLayout&gt;
    &lt;/HorizontalScrollView&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <h2>The question</h2>
  
  <p>Why does it occur?</p>
  
  <p>Both methods should have the same result, as both sample from the same source and using the same factor. </p>
  
  <p>I've tried to play with the downsampling method, but nothing has helped. </p>
  
  <p>Using the inDensity (instead of inSampleSize) seems to fix it, but I'm not sure what to set for it . i think that for outside images (from the internet for example), i can set it to the screen density multiplied by the sample size i wish to use.</p>
  
  <p>But is it even a good solution? what should i do in the case the images are inside the resources folder (i don't think there is a function to get which density folder a bitmap is located at) ? why does it work while using the recommended way (talked about <a href="http://developer.android.com/training/displaying-bitmaps/load-bitmap.html" rel="nofollow noreferrer">here</a>) doesn't work well ?</p>
  
  <hr>
  
  <p>EDIT: I've found out a trick to get which density is used for a drawable you get from the resources (<a href="https://stackoverflow.com/a/8577411/878126">link here</a>) . however, it isn't future proof, as you need to be specific to the density to detect.</p>