j2emanue
j2emanue

Reputation: 62519

ImgIX in Android - how to use dpr for scaling per density?

my company recently got imgIX but im not sure how to use the dpr .

I mean its straight forward the actual implementation but how do i know which dpr value to use to match android density. docs say that dpr goes from 1 to 8. Android has ldpi, mdpi ... xxxxhdpi. so how can i match these up ? Also, its so hard to believe they dont have a android documentation section to show usage in android given the different densities.

Upvotes: 1

Views: 399

Answers (1)

j2emanue
j2emanue

Reputation: 62519

Reading the imgix docs about sourcesets .. so i ended up with the following scheme:

i created two integer files in values resouce directory to store the dpr values.

so like this:

*

    values
        |
        integers.xml 
values-mdpi
        |
        integers.xml 
values- hdpi
        |
        integers.xml

//etc for ALL screen densities as android scales downwards so have to do all densities

*

inside each of these integers file i find the correct dpr to use based on density. so in values folder i use 2 as the default and then mdpi and hdpi will have 1 and 1.5 etc ...

here is the scale factor to use:

From this:

ldpi = 0.00625 * 120 -> 0.75 mdpi = 0.00625 * 160 -> 1.0 hdpi =0.00625 * 240 -> 1.5 xhdpi = 0.00625 * 320 -> 2.0 xxhdpi = 0.00625 * 480 -> 3.0 xxxhdpi = 0.00625 * 640 -> 4.0


from here So for example the default is:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="dpr">2</integer>
</resources>

and mdpi:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="dpr">1</integer>
</resources>

and hdpi:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <Integer name="dpr">2</integer> //this should be 1.5 but lazy to convert to double
</resources>

then in code when i am buliding the imgix image i call the respectively value like this:

params.put("dpr", "" + getResources().getInteger(R.integer.dpr));

the entire call would look something like this in a custom view (in my case):

final URLBuilder builder = new URLBuilder(baseUrl);
        params.put("w", String.valueOf(getWidth()));
        params.put("h", String.valueOf(getHeight()));
        params.put("q", IMGIX_QUALITY);
        params.put("auto", "compress");
        params.put("dpr", "" + getResources().getInteger(R.integer.dpr));
        params.put("fix", "max");
        String url = builder.createURL(getImageId(), params);

        //then send this off to picasso to cache. etc ...

Upvotes: 0

Related Questions