Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to scale an image yourself, put a single copy in:</p> <pre><code>res/drawable-nodpi </code></pre> <p>Make sure that resource is in no other folders. </p> <p>Load the bitmap like this:</p> <pre><code>Bitmap bitmap = BitmapFactory.decodeResource( context.getResources(), R.drawable.my_image ); </code></pre> <p>Resize it like this:</p> <pre><code>bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true); </code></pre> <p>Then draw it to your canvas:</p> <pre><code>canvas.drawBitmap(bitmap, 0, 0, null); </code></pre> <p>That's the least you need to know to get the job done.</p> <p><strong>Additional notes</strong></p> <p>Normally, Android chooses your image from one of the res/drawable folders based on the current screen density.</p> <p>Most image formats don't have density information, so Android makes assumptions about the density of the bitmap based on the folder it was loaded from.</p> <p>That means it can do it's own scaling to match the density of the loaded bitmap with the canvas density. </p> <p>Android checks and matches densities once when you load the bitmap, and <strong>again</strong> when you draw the bitmap.</p> <p>When you are doing your own scaling you generally want to prevent the OS from interfering. There are several ways you can do it: </p> <p><strong>1. Use res/drawable-nodpi</strong></p> <p>This is the strategy I outlined in the quick example above. If you want to package your image as a resource, and still control the scaling yourself, put it here, and only here:</p> <pre><code>res/drawable-nodpi </code></pre> <p>If you need to support Cupcake, put a copy in each of these folders instead:</p> <pre><code>res/drawable-nodpi-v4 res/drawable </code></pre> <p>The decoder is automatically provided with options to prevent scaling. Android then sets the image density to to match the screen, so it won't scale when you draw it either. <a href="http://developer.android.com/guide/topics/graphics/2d-graphics.html#drawables" rel="nofollow noreferrer">The compiler will apply lossless compression to images placed in res/drawable-xxx folders</a>. </p> <p><strong>2. Use res/raw</strong></p> <p>You can leave the image untouched by the compiler if you put it in:</p> <pre><code>res/raw </code></pre> <p>But then you need to be careful to prevent scaling at runtime:</p> <pre><code>BitmapFactory.Options options = new BitmapFactory.Options(); options.inScaled = false; // Prevents scaling at decode time. Bitmap bitmap = BitmapFactory.decodeResource( context.getResources(), R.raw.my_image, options ); bitmap.setDensity(Bitmap.DENSITY_NONE); // Prevents scaling at draw time. </code></pre> <p><strong>3. Use the assets folder</strong></p> <p>If you put your images in the <strong>assets</strong> folder, instead of in <strong>res</strong>, you can use a real folder structure to organize them. Load a bitmap asset like this: </p> <pre><code>Bitmap bitmap = BitmapFactory.decodeStream( context.getAssets().open( "myfolder/my_image.png", AssetManager.ACCESS_BUFFER ) ); </code></pre> <p>The compiler won't touch it, it loads without scaling, and it's density is set to match the screen, so it won't get scaled when you draw it either. </p> <p>(PERFORMANCE NOTE: Before Android loads an image, it has to locate the file. <a href="https://stackoverflow.com/questions/9563373/the-reason-for-assets-and-raw-resources-in-android">A resource is faster to locate than an asset</a>, so there may be some performance penalty if you package your images as assets.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload