Yes I want to keep the millimeter unit as a supported mesurement unit in 
CN1, don't worry. But I also want to add dp and sp as possible additional 
units. In any case, I think it makes more sense to make getDeviceDensity() 
directly 
return the PPI value (so the value in metrics.densityDpi in Android for 
example or a more exact measure based on xdpi and ydpi. So rather than 
having a value of 30 for DENSITY_MEDIUM devices, the density value would be 
160). I made the changes into the default CodenameOneImplementation class 
(the one into CodenameOne and all related classes (the Display and 
MultipleImages related ones)), now I will do it into the 
CodenameOneImplementation of each platform port that override the modified 
functions.
That way, the convertToPixels function can just be wrote like this (Remark: 
getDevicePPI() function is the getDeviceDensity() new function that return 
the density in PPI (I renamed it to be sure not to miss any dependency into 
my changes):

 public int convertToPixels(int dipCount, boolean horizontal) { 
        return convertMillimetersToPixels(dipCount, horizontal);
    }
    
    public int convertMillimetersToPixels(double millimeters, boolean 
horizontal) { 
    return (int) (getDevicePPI()/25.4*millimeters); //TODO: use 
Math.round() rather than int casting? Would be more precise but affect 
performances a bit as int casting is probably quicker than rounding 
    }




On Tuesday, May 15, 2018 at 6:29:50 AM UTC+2, Shai Almog wrote:
>
> The reason we work with millimeters is mostly for uniformity across 
> platforms as a universal standard. Changing to the Android DIP standard 
> would be problematic as all the tooling/theming etc is based around 
> millimeters so we can expose that in an API but not leverage this. It 
> wouldn't help. 
>
> Are you talking about this method? :
>
>     @Override
>     public int getDeviceDensity() {
>         DisplayMetrics metrics = new DisplayMetrics();
>         if (getActivity() != null) {
>             getActivity().getWindowManager().getDefaultDisplay().
> getMetrics(metrics);
>         } else {
>             metrics = getContext().getResources().getDisplayMetrics();
>         }
>
>         if(metrics.densityDpi < DisplayMetrics.DENSITY_MEDIUM) {
>             return Display.DENSITY_LOW;
>         }
>
>         if(metrics.densityDpi < 213) {
>             return Display.DENSITY_MEDIUM;
>         }
>
>         // 213 == TV
>         if(metrics.densityDpi >= 213 &&  metrics.densityDpi <= 
> DisplayMetrics.DENSITY_HIGH) {
>             return Display.DENSITY_HIGH;
>         }
>
>         if(metrics.densityDpi > DisplayMetrics.DENSITY_HIGH && 
> metrics.densityDpi 
> < 400) {
>             return Display.DENSITY_VERY_HIGH;
>         }
>
>         if(metrics.densityDpi >= 400 && metrics.densityDpi < 560) {
>             return Display.DENSITY_HD;
>         }
>
>         if(metrics.densityDpi >= 560 && metrics.densityDpi <= 640) {
>             return Display.DENSITY_2HD;
>         }
>         if(metrics.densityDpi > 640) {
>             return Display.DENSITY_4K;
>         }
>
>         return Display.DENSITY_MEDIUM;
>     }
>
>
> In this case using the densityDpi field makes a lot of sense as it's the 
> officially sanctioned Android API that does exactly this. 
>
> I'm talking about this method only:
>
>     public int convertToPixels(int dipCount, boolean horizontal) {
>         DisplayMetrics dm = getContext().getResources().getDisplayMetrics
> ();
>         float ppi = dm.density * 160f;
>         return (int) (((float) dipCount) / 25.4f * ppi);
>     }
>
> Right now this is defensive code designed against the bugs in dm.xdpi/ydpi 
> which I'm assuming you want to use instead. To work correctly you would 
> need to guard against misconfigured xdpi by checking that the range 
> difference isn't too big.
>
> Notice that this method is usually invoked with *1000 of the value asked 
> and then converted to a floating point value for higher accuracy.
>

-- 
You received this message because you are subscribed to the Google Groups 
"CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/codenameone-discussions/9cc4d45d-75f4-488b-8b9f-fb4c9ccc7de3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to