2012-08-29 59 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; 
    } 
}