Android 取得畫面解析度

取得畫面解析後才方便修改UI的比例來作調整,修改UI大小的方式在前一篇"Android setWidth setHeight 設定UI寬高"當中有提到,就可以配合著取得畫面解析度來作調整。

screenWidth就是螢幕的寬,screenHeight就是螢幕的高,所call的getScreenScaleType就是取得畫面比例
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
String scapeType = getScreenScaleType(screenWidth, screenHeight);
System.out.println("scapeType = "+scapeType);

//getScreenScaleType
public String getScreenScaleType(int width, int height){
String scaleType = "unknow";
float scale = (float) 0.0;
boolean landspace = false;

if(width > height){
landspace = true;
}else{
landspace = false;
}

if(landspace == true){
scale = (float)width/(float)height;
}else{
scale = (float)height/(float)width;
}
// System.out.println("width = "+width+" || height = "+height+" || scaleType = "+scaleType);

if(scale >= 1.76 && scale <= 1.8){ //WVGA800 or WQVGA432 if(landspace == true && width == 432){ scaleType = "WQVGA432landspace"; }else if(landspace == true && width == 854){ scaleType = "WQVGA854landspace"; }else if(landspace == false && height == 432){ scaleType = "WQVGA432"; }else if(landspace == false && height == 854){ scaleType = "WQVGA854"; }else{ scaleType = "5:8"; } }else if(scale >= 1.66 && scale <= 1.78){ //WVGA800 or QWVGA400 if(landspace == true && width == 400){ scaleType = "QWVGA400landspace"; }else if(landspace == true && width == 800){ scaleType = "WVGA800landspace"; }else if(landspace == false && height == 400){ scaleType = "QWVGA400"; }else if(landspace == false && height == 400){ scaleType = "WVGA800"; }else{ scaleType = "3:5"; } }else if(scale == 1.5){ //HVGA if(landspace == true && width == 480){ scaleType = "HVGAlandspace"; }else if(landspace == false && height == 480){ scaleType = "HVGA"; }else{ scaleType = "2:3"; } }else if(scale >= 1.3 && scale <= 1.4){ //QVGA if(landspace == true && width == 320){ scaleType = "QVGAlandspace"; }else if(landspace == false && height == 480){ scaleType = "QVGA"; }else{ scaleType = "3:4"; } } System.out.println("width = "+width+" || height = "+height+" || scaleType = "+scaleType); return scaleType; }


不過在解析度這部份還是有個地方不太了解,由於我也沒有手機可以來作實測沒辦法了解到詳細狀況,目前在emulator裡面看到的狀況如下
HVGA =>320*480(width*height) || 實測取得為 320*480(width*height)
QVGA =>240*320(width*height) || 實測取得為 320*427(width*height)
WQVGA400 =>240*400(width*height) || 實測取得為 320*533(width*height)
WQVGA432 =>240*432(width*height) || 實測取得為 320*480(width*height)
WVGA800 =>480*800(width*height) || 實測取得為 320*533(width*height)
WVGA854 =>480*854(width*height) || 實測取得為 320*569(width*height)

紅色的部份為實測時發現不同的部份,不過目前也沒有這麼多的device來確認究竟是哪邊的問題,好消息是如果真的都與實測相同那解析度在寬的部份都是相同的(皆為320),歸類起來高也只有4種(427, 480, 533, 569),在device上面目前只有看到320*427(HTC Wildfire)、320*480(HTC hero)、320*533(HTC Desire)是比較確定的,其他的...除非有人願意借我測看看吧。

相關連結
Android Developers
Android Developers - Activity
Android setWidth setHeight 設定UI寬高

沒有留言:

張貼留言