Question

I have this drawable folders in my app:

drawable

drawable-hdpi, mdpi, ldpi, xhdpi and xxhdpi.

drawable-sw240dp-land-xxxhdpi

drawable-sw360dp-land-xxhdpi

drawable-sw6000dp-land-xhdpi

and I have the same drawable files the same size in all of those folders. I need those folders cause of density in landscape.

Is there any way that I can avoid coping same files in different drawable folders?

Thanks in advance

Was it helpful?

Solution

Is there any way that I can avoid coping same files in different drawable folders?

Start by getting rid of the ones that you do not need:

  • res/drawable/ is unnecessary as it will never be used, since you have drawables in the various density buckets. Android will always scale one of your res/drawable-*dpi/ resources to match the screen density, for densities that you are not using (e.g., -tvdpi/).

  • If res/drawable-sw360dp-land-xxhdpi/ has identical icon files to the ones in res/drawable-xxhdpi/, then you do not need res/drawable-sw360dp-land-xxhdpi/.

  • If res/drawable-sw6000dp-land-xhdpi/ has identical icon files to the ones in res/drawable-xhdpi/, then you do not need `res/drawable-sw6000dp-land-xhdpi/.

  • And so on

Beyond that, you can use bitmap resource aliases to allow one bitmap file to be represented in multiple resource directories.

OTHER TIPS

Yes, there is, but at the cost of some minor distortions and careful layout design. What you can do is drop your high-res drawable into drawable-nodpi; Android will pull the drawable from that folder without scaling. Next, create a series of dimension files, dimens.xml, for the various screen sizes. For example, for normal size screens:

<resources>    
    <dimen name="dim_0dp">0dp</dimen>
    <dimen name="dim_1dp">1dp</dimen>
    <dimen name="dim_2dp">2dp</dimen>
    <dimen name="dim_3dp">3dp</dimen>
    <dimen name="dim_4dp">4dp</dimen>
    <dimen name="dim_5dp">5dp</dimen>
    <dimen name="dim_6dp">6dp</dimen>
    <dimen name="dim_7dp">7dp</dimen>
    <dimen name="dim_8dp">8dp</dimen>
    <dimen name="dim_9dp">9dp</dimen>
    ...
</resources>

And, the resource file for a sw-600dp tablet will look like this for example:

<resources>    
    <dimen name="dim_0dp">0dp</dimen>
    <dimen name="dim_1dp">1.5dp</dimen>
    <dimen name="dim_2dp">3dp</dimen>
    <dimen name="dim_3dp">4.5dp</dimen>
    <dimen name="dim_4dp">6dp</dimen>
    ...
</resources>

Then, every where in your layout that you use an ImageView or you set the background using a drawable, make sure to specify the width and height based on your dimens file. For example:

<ImageView 
         android:layout_width="@dimens/dim_10dp"
         android:layout_height="@dimen/dim_20dp" />

So, on a phone, the image will get scaled to 10dp and 20dp; whereas, on a 600dp tablet it will get scaled to 15dp and 30dp. This means some minor distortions but since you are scaling down and not up, the distortions are minimal and hard to detect to most users.

This was a strategy we used for shipping three successful games, otherwise our APK would've been massive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top