Soru & Cevap

DisplayMetrics Bug? ...

05.08.2014 - 07:51

DisplayMetrics bazı telefonlarda yanlış değerler döndürüyor. Örnek olarak aşağıdaki kod çoğu cihazda düzgün çalışırken HTC one x'te  diagonal ekran boyutunu 6.894inç veriyor ki olması gereken değer 4.65inch. bu sorunun bir çözümü varmı yoksa bu bir bugmı? Android telefonlarda ekran boyutunun kaç inç olduğunu kodlama ile bulabilirmiyim? Yoksa boşunamı uğraşıyorum. Yardımlarınız için şimdiden teşekkürler. 

[code]DisplayMetrics metrics = new DisplayMetrics();         

         getWindowManager().getDefaultDisplay().getMetrics(metrics);
        
         float widthDpi = metrics.xdpi;
         float heightDpi = metrics.ydpi;
         int widthPixels = metrics.widthPixels;
         int heightPixels = metrics.heightPixels;
         
         widthInches = widthPixels / widthDpi;
         heightInches = heightPixels / heightDpi;
       
        diagonalInches = Math.sqrt(
           (widthInches * widthInches) 
           + (heightInches * heightInches));
       
       if (diagonalInches >= 9) {
            //10" tablet
        } 
        else if (diagonalInches >= 6.9) {
            //7" tablet            
        }else if (diagonalInches >= 4.9) {
            //5" phone
        }else if (diagonalInches>4.3&&diagonalInches<4.9) {
            //Device is a 4,65" phone            
        }else if (diagonalInches>=3.7&&diagonalInches<=4.3) {
           //Device is a 4" phone
        }[/code]

57 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-2335-1379625106.jpg
rocxteady
05.08.2014 - 08:28

[code]

double widthPixels;

        double heightPixels;

        WindowManager w = this.getWindowManager();

        Display d = w.getDefaultDisplay();

        DisplayMetrics metrics = new DisplayMetrics();

        d.getMetrics(metrics);

        // since SDK_INT = 1;

        widthPixels = metrics.widthPixels;

        heightPixels = metrics.heightPixels;

        // includes window decorations (statusbar bar/menu bar)

        if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)

            try {

                widthPixels = (Integer) Display.class.getMethod("getRawWidth")

                        .invoke(d);

                heightPixels = (Integer) Display.class

                        .getMethod("getRawHeight").invoke(d);

            } catch (Exception ignored) {

            }

        // includes window decorations (statusbar bar/menu bar)

        if (Build.VERSION.SDK_INT >= 17)

            try {

                Point realSize = new Point();

                Display.class.getMethod("getRealSize", Point.class).invoke(d,

                        realSize);

                widthPixels = realSize.x;

                heightPixels = realSize.y;

            } catch (Exception ignored) {

            }

        System.out.println(widthPixels + " " + heightPixels);

        DisplayMetrics dm = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(dm);

        double x = Math.pow(widthPixels / dm.xdpi, 2);

        double y = Math.pow(heightPixels / dm.ydpi, 2);

        double screenInches = Math.sqrt(x + y);

        Log.d("debug", "Screen inches : " + screenInches);

[/code]

Barış
06.08.2014 - 09:53
Bu kodda 6.894inç değeri verdi. Sorun metrics.xdpi ve metrics.ydpi metodlarının bazı telefonlarda metrics.densityDpi metodundan farklı bir değer döndürüyor olmasıymış.