2011-06-24 11 views
12

Jak uzyskać gęstość ekranu? Próbowałem z następującego kodu. Mam gęstość jako 1.0. Zmieniłem emulator TO HVGA, QVGA i WVGA nadal otrzymuję ten sam wynik.Jak uzyskać gęstość ekranu?

int density = getResources().getDisplayMetrics().density; 

Jak uzyskać rzeczywistą gęstość ekranu mojego emulatora.

Odpowiedz

23

Sprawdź z tym kodem,

DisplayMetrics metrics = new DisplayMetrics();  
getWindowManager().getDefaultDisplay().getMetrics(metrics);  
int screenDensity = metrics.densityDpi;  
12

Jeśli masz tylko dostęp do kontekstu, a nie rzeczywistego obiektu aktywny, nie będzie w stanie wykorzystać getWindowManager().

musiałem użyć tego podejścia:

DisplayMetrics metrics = new DisplayMetrics(); 
WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); 
wm.getDefaultDisplay().getMetrics(metrics); 
1
DisplayMetrics displaymetrics = new DisplayMetrics(); 
    float density= getResources().getDisplayMetrics().density; 
    if(density==1.0){ **//This is for mdpi** 

     params.width = 70; 
     params.height=70; 
     dynamicSeekBar.setLayoutParams(params); 
     //    Toast.makeText(this, "1.0", Toast.LENGTH_LONG).show(); 

    }else if(density==1.5){ **//This is for hdpi** 

     params.width =110; 
     params.height=110; 
     dynamicSeekBar.setLayoutParams(params); 
     //    Toast.makeText(this, "1.5", Toast.LENGTH_LONG).show(); 
    }else if(density==2.0){ **//This is for xhdpi** 

     params.width = 177; 
     params.height=175; 
     dynamicSeekBar.setLayoutParams(params); 
     //    Toast.makeText(this, "2.0", Toast.LENGTH_LONG).show(); 

    } 
3

spróbować.

http://www.androidwarriors.com/2016/01/how-to-find-different-devices-screen.html

public static int differentDensityAndScreenSize(Context context) { 
    int value = 20; 
    String str = ""; 
    if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) { 
    switch (context.getResources().getDisplayMetrics().densityDpi) { 
    case DisplayMetrics.DENSITY_LOW: 
    str = "small-ldpi"; 
    value = 20; 
    break; 
    case DisplayMetrics.DENSITY_MEDIUM: 
    str = "small-mdpi"; 
    value = 20; 
    break; 
    case DisplayMetrics.DENSITY_HIGH: 
    str = "small-hdpi"; 
    value = 20; 
    break; 
    case DisplayMetrics.DENSITY_XHIGH: 
    str = "small-xhdpi"; 
    value = 20; 
    break; 
    case DisplayMetrics.DENSITY_XXHIGH: 
    str = "small-xxhdpi"; 
    value = 20; 
    break; 
    case DisplayMetrics.DENSITY_XXXHIGH: 
    str = "small-xxxhdpi"; 
    value = 20; 
    break; 
    case DisplayMetrics.DENSITY_TV: 
    str = "small-tvdpi"; 
    value = 20; 
    break; 
    default: 
    str = "small-unknown"; 
    value = 20; 
    break; 
    } 

    } else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) { 
    switch (context.getResources().getDisplayMetrics().densityDpi) { 
    case DisplayMetrics.DENSITY_LOW: 
    str = "normal-ldpi"; 
    value = 82; 
    break; 
    case DisplayMetrics.DENSITY_MEDIUM: 
    str = "normal-mdpi"; 
    value = 82; 
    break; 
    case DisplayMetrics.DENSITY_HIGH: 
    str = "normal-hdpi"; 
    value = 82; 
    break; 
    case DisplayMetrics.DENSITY_XHIGH: 
    str = "normal-xhdpi"; 
    value = 90; 
    break; 
    case DisplayMetrics.DENSITY_XXHIGH: 
    str = "normal-xxhdpi"; 
    value = 96; 
    break; 
    case DisplayMetrics.DENSITY_XXXHIGH: 
    str = "normal-xxxhdpi"; 
    value = 96; 
    break; 
    case DisplayMetrics.DENSITY_TV: 
    str = "normal-tvdpi"; 
    value = 96; 
    break; 
    default: 
    str = "normal-unknown"; 
    value = 82; 
    break; 
    } 
    } else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) { 
    switch (context.getResources().getDisplayMetrics().densityDpi) { 
    case DisplayMetrics.DENSITY_LOW: 
    str = "large-ldpi"; 
    value = 78; 
    break; 
    case DisplayMetrics.DENSITY_MEDIUM: 
    str = "large-mdpi"; 
    value = 78; 
    break; 
    case DisplayMetrics.DENSITY_HIGH: 
    str = "large-hdpi"; 
    value = 78; 
    break; 
    case DisplayMetrics.DENSITY_XHIGH: 
    str = "large-xhdpi"; 
    value = 125; 
    break; 
    case DisplayMetrics.DENSITY_XXHIGH: 
    str = "large-xxhdpi"; 
    value = 125; 
    break; 
    case DisplayMetrics.DENSITY_XXXHIGH: 
    str = "large-xxxhdpi"; 
    value = 125; 
    break; 
    case DisplayMetrics.DENSITY_TV: 
    str = "large-tvdpi"; 
    value = 125; 
    break; 
    default: 
    str = "large-unknown"; 
    value = 78; 
    break; 
    } 

    } else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) { 
    switch (context.getResources().getDisplayMetrics().densityDpi) { 
    case DisplayMetrics.DENSITY_LOW: 
    str = "xlarge-ldpi"; 
    value = 125; 
    break; 
    case DisplayMetrics.DENSITY_MEDIUM: 
    str = "xlarge-mdpi"; 
    value = 125; 
    break; 
    case DisplayMetrics.DENSITY_HIGH: 
    str = "xlarge-hdpi"; 
    value = 125; 
    break; 
    case DisplayMetrics.DENSITY_XHIGH: 
    str = "xlarge-xhdpi"; 
    value = 125; 
    break; 
    case DisplayMetrics.DENSITY_XXHIGH: 
    str = "xlarge-xxhdpi"; 
    value = 125; 
    break; 
    case DisplayMetrics.DENSITY_XXXHIGH: 
    str = "xlarge-xxxhdpi"; 
    value = 125; 
    break; 
    case DisplayMetrics.DENSITY_TV: 
    str = "xlarge-tvdpi"; 
    value = 125; 
    break; 
    default: 
    str = "xlarge-unknown"; 
    value = 125; 
    break; 
    } 
    } 
// The Toast will show the Device falls in Which Categories. 
Toast.makeText(MainActivity.this, ""+str, Toast.LENGTH_SHORT).show(); 

    return value; 
} 
+0

To najlepsza odpowiedź. Dobra robota! – marienke

+0

Dobra odpowiedź (Y) – Saveen