Are the dimensions in the "drawable" folder automatically resized?

I have a 1280x720px image in res / drawable folder (not drawable-hdpi, drawable-ldpi, etc.). But at run time, the size is 2560x1440 pixels. How is this possible? Does android change the images in the dropdown folder?

Here is the code:

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.start_image_left);
    int h = bitmap.getHeight(), w = bitmap.getWidth();

      

I am testing this on Motorola Moto G gen2.

+3


source to share


2 answers


There is really no reason why you should put the bitmap in the root directory drawable

. It should generally only be used for XML drawings. Bitmaps in the catalog drawable

will essentially be treated as if they were in drawable-mdpi

(scaled down proportionally for other densities).



If your goal is to have a bitmap with the same pixel size at all densities, you need to place it in drawable-nodpi

. This will cause it to not scale.

+7


source


You're right, Android will automatically scale your image based on the current device configuration. If, however, you don't want this behavior, you can create a bitmap resource file, put it in a folder drawable

and point it to the pushed one you want to use like this

<bitmap xmlns:android="http://schemas.android.com/apk/res/android android:src="@drawable/yourDrawableHere" android:gravity="center" />

By specifying the center of gravity, android will not scale your image no matter what.



To use this bitmap resource file in your code, extract it like so:

Drawable drawable = getResources().getDrawable(R.drawable.yourBitmaoResourceFileName);

0


source







All Articles