2012-08-29 64 views
20

我是新來的PhoneGap和麪臨的一個問題的多個圖像的分辨率和密度,我作出了PhoneGap的應用程序,它會在不同的屏幕尺寸和不同屏幕分辨率的多平臺設備上運行,所以我要加載的圖片根據屏幕分辨率的不同分辨率。支持在PhoneGap的

這可以在android系統通過簡單地在華電國際,MDPI和LDPI文件夾把不同分辨率的您的圖像來實現它(機器人)抓取的圖像自動根據設備屏幕分辨率。但如何在phonegap中做到這一點。

我已經看到了很多關於響應網頁設計的文章,他們都說關於定位的網頁,但其中不上的元素已經講述瞭如何屏幕分辨率的基礎上進行圖像。

感謝我前進。

編輯的問題

我用下面的HTML

<div id="header" data-role="header" data-position="fixed"> 
<img alt="app_icon" src="pictures/app_logo.png" display="inline" class="align-left" /> 
<img alt="brand_icon" src="pictures/company_logo.png" display="inline" class="align-right" /><h1></h1> 
</div> 

現在我有內部資產/ WWW /圖片文件夾的圖像代碼。這個文件夾包括相同的分辨率app_logo.png和company_logo.png的2個圖像和更高分辨率app_logo_big.png和company_logo_big.png 2個圖像現在通過媒體查詢我就知道屏幕尺寸和應用樣式,但據我所知我不能從CSS更改img src。那麼現在我將如何使用這些不同分辨率的圖像

回答

28

然後通過動態的jQuery更改圖像ES爲不同分辨率:

HTML:

<div id="header" data-role="header" data-position="fixed"> 
    <span id="app-icon"></div> 
    <span id="brand-icon"></div> 
</div> 

CSS:

/* Low density (120), mdpi */ 
@media screen and (-webkit-device-pixel-ratio: 0.75) { 
    #app-icon { background-image:url(pictures/ldpi/app-icon.png); } 
    #brand-icon { background-image:url(pictures/ldpi/brand-icon.png); } 
} 

/* Medium density (160), mdpi */ 
@media screen and (-webkit-device-pixel-ratio: 1) { 
    #app-icon { background-image:url(pictures/mpi/app-icon.png); } 
    #brand-icon { background-image:url(pictures/mdpi/brand-icon.png); } 
} 

/* High density (240), hdpi */ 
@media screen and (-webkit-device-pixel-ratio: 1.5) { 
    #app-icon { background-image:url(pictures/hdpi/app-icon.png); } 
    #brand-icon { background-image:url(pictures/hdpi/brand-icon.png); } 
} 

/* Extra high density (320), xhdpi */ 
@media screen and (-webkit-device-pixel-ratio: 2) { 
    #app-icon { background-image:url(pictures/xdpi/app-icon.png); } 
    #brand-icon { background-image:url(pictures/xdpi/brand-icon.png); } 
} 

如果你想通過過濾,

取向 - and (orientation: landscape)

設備寬度and (min-device-width : 480px) and (max-device-width : 854px)

例子:

@media screen and (-webkit-device-pixel-ratio: 1.5) and (min-device-width : 640px) and (max-device-width : 960px) and (orientation: landscape) { 
    /* Your style here */ 
} 
+0

請我的代碼 – prateek

+0

更新的問題,哎你使用哪一種解決方案?從上面? – MDroid

+0

我推薦使用'-webkit敏設備象素ratio'代替固定比率'-webkit設備象素ratio'。 – givanse

3

創建對更多大小的支持是一個問題,但您可以在CSS中使用@media queries來修復它。檢查此示例代碼:

/* iPads (landscape) ----------- */ 
@media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px) 
    and (orientation : landscape) { 
     /* Styles */ 
} 

/* iPads (portrait) ----------- */ 
@media only screen 
    and (min-device-width : 768px) 
    and (max-device-width : 1024px) 
    and (orientation : portrait) { 
     /* Styles */ 
} 

/* Desktops and laptops ----------- */ 
@media only screen 
    and (min-width : 1224px) { 
     /* Styles */ 
} 

/* Large screens ----------- */ 
@media only screen 
    and (min-width : 1824px) { 
     /* Styles */ 
} 

通過此代碼,您可以添加對所有設備的支持。 Check this link for getting more code for more browsers

功能,其可以使用:

  • 寬度和高度:(min-device-width : 768px) and (max-device-width : 1024px)
  • 定位:(orientation: landscape)(orientation: portrait)
  • 設備的像素比例:(-webkit-device-pixel-ratio: 1.5)

編輯

HTML:

<div id="header" data-role="header" data-position="fixed"> 
<span id="app_icon" alt="app_icon" src="pictures/app_logo.png" display="inline" class="align-left"></span> 
<span id="brand_icon" alt="brand_icon" src="pictures/company_logo.png" display="inline" class="align-right"></span><h1></h1> 
</div> 

變化imgspan並添加標識。

CSS:

@media screen and (-webkit-device-pixel-ratio: 0.75) { 
    #app_icon { 
    width: 100px; /* Example size */ 
    height: 100px; /* Example size */ 
    background: url(pictures/app_logo_small.png); 
    } 
} 

@media screen and (-webkit-device-pixel-ratio: 1) { 
    #app_icon { 
    width: 150px; /* Example size */ 
    height: 150px; /* Example size */ 
    background: url(pictures/app_logo_medium.png); 
    } 
} 

@media screen and (-webkit-device-pixel-ratio: 1.5) { 
    #app_icon { 
    width: 200px; /* Example size */ 
    height: 200px; /* Example size */ 
    background: url(pictures/app_logo_large.png); 
    } 
} 

@media screen and (-webkit-device-pixel-ratio: 2) { 
    #app_icon { 
    width: 300px; /* Example size */ 
    height: 300px; /* Example size */ 
    background: url(pictures/app_logo_xlarge.png); 
    } 
} 

有了這個例子,你可以改變你的代碼,並修復它。希望這個幫助!

HTML:

<div id="header" data-role="header" data-position="fixed"> 
    <img id="app-icon" src="pictures/app_logo.png" display="inline" /> 
</div> 

的Javascript:

$(document).ready(function() { 
    if(window.devicePixelRatio == 0.75) { 
    $("#app-icon").attr('src', '/images/lpdi/app-icon.png'); 
    } 
    else if(window.devicePixelRatio == 1) { 
      $("#app-icon").attr('src', '/images/mdi/app-icon.png'); 
    } 
    else if(window.devicePixelRatio == 1.5) { 
    $("#app-icon").attr('src', '/images/hpdi/app-icon.png'); 
    } 
    else if(window.devicePixelRatio == 2) { 
       $("#app-icon").attr('src', '/images/xpdi/app-icon.png'); 
    } 
} 

通過CSS:使用媒體Queri

+0

請檢查我的更新問題與代碼 – prateek

0

我認爲你必須通過屏幕密度來劃分報道屏幕尺寸來獲得媒體查詢寬度和高度尺寸。

3

你也可以做到這一點使用handlebars幫手,其中更少的代碼密集型的,在我看來的推薦方法:

if (screen.width <= 480) { 
    imgFolder = 'img/low/'; 
} 
else { 
    imgFolder = 'img/high/'; 
} 


Handlebars.registerHelper('imgFolder', function() { 
    return imgFolder 
}); 

img/low/img/high包含具有相同名稱的不同分辨率的圖像。

然後在你的模板:

<img src="{{imgFolder}}yourImage.png" /> 

當然,你必須根據你的設備的應用程序的目標設置屏幕大小值。

附錄: 如果你沒有1:科爾多瓦瀏覽器1像素映射(不推薦 - 你的形象將有一個模糊/不清晰的樣子)screen.width將不同於瀏覽器的寬度(window.innerWidth)和你必須使用$(window).width()window.innerWidth。您可能可以使用媒體查詢修復錯誤的映射。

1

我發現我不得不使用這些媒體查詢添加對0.5,1,1.3,1.5,2和3像素比率的支持。

注意我正在使用-webkit-min-device-pixel-ratio而不是-webkit-device-pixel-ratio。 我發現在我的其中一個測試設備(Galaxy Tab 3 - 8「)上,像素比例爲1.3,並沒有採用我在phonegap應用中設置的任何特定樣式。

@media screen and (-webkit-min-device-pixel-ratio: 0.5) { 
    #app_icon { 
     width:64px; 
     height:64px; 
     background: url('../images/bigstart.png') no-repeat center bottom; 
     background-size: 64px 64px; 
    } 
} 
@media screen and (-webkit-min-device-pixel-ratio: 1) { 
    #app_icon { 
     width:64px; 
     height:64px; 
     background: url('../images/bigstart.png') no-repeat center bottom; 
     background-size: 64px 64px; 
    } 
} 
} 
@media screen and (-webkit-min-device-pixel-ratio: 1.3) { 
    #app_icon { 
     width:64px; 
     height:64px; 
     background: url('../images/[email protected]') no-repeat center bottom; 
     background-size: 64px 64px; 
    } 
} 
@media screen and (-webkit-min-device-pixel-ratio: 1.5) { 
    #app_icon { 
     width:64px; 
     height:64px; 
     background: url('../images/[email protected]') no-repeat center bottom; 
     background-size: 64px 64px; 
    } 
} 
@media screen and (-webkit-min-device-pixel-ratio: 2) { 
    #app_icon { 
     width:64px; 
     height:64px; 
     background: url('../images/[email protected]') no-repeat center bottom; 
     background-size: 64px 64px; 
    } 
} 
@media screen and (-webkit-min-device-pixel-ratio: 3) { 
    #app_icon { 
     width:64px; 
     height:64px; 
     background: url('../images/[email protected]') no-repeat center bottom; 
     background-size: 64px 64px; 
    } 
}