热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

支持多种分辨率和密度的图像在phonegap-supportingmultipleresolutionanddensityofimagesinphonegap

Iamnewtophonegapandfacingaproblem,Iammakingaphonegapappwhichwillrunonmultiplepla

I am new to phonegap and facing a problem, I am making a phonegap app which will run on multiple platform devices of different screen size and different screen resolution so I have to load images of different resolution depending on screen resolution.

我是phonegap新手,遇到了一个问题,我正在制作一个phonegap app,它可以在不同屏幕大小、不同屏幕分辨率的多个平台设备上运行,所以我必须根据屏幕分辨率加载不同分辨率的图像。

this can be achieved in android by simply putting your images of different resolution in hdpi, mdpi and ldpi folder and it(android) fetches images automatically depending on devices screen resolution. But how to do this in phonegap.

这可以在android中实现,只需将不同分辨率的图像放在hdpi、mdpi和ldpi文件夹中,并根据设备屏幕分辨率自动获取图像。但是如何在phonegap中做到这一点。

I have seen lot of articles on responsive web design they all say about positioning the elements on web page but non of them has told about how to place images on the basis of screen resolutions.

我看过很多关于响应性网页设计的文章他们都说要把元素放在网页上,但是没有一篇是关于如何在屏幕分辨率的基础上放置图片的。

thanks i advance.

谢谢我的进步。

edited question

编辑的问题

i have used following code for html

我使用了以下html代码


now I have images inside assets/www/pictures folder. this folder consists of 2 images of same resolution app_logo.png and company_logo.png and 2 images of higher resolution app_logo_big.png and company_logo_big.png now through media queries i will know the screen size and apply the styles but as far as i know i cannot change img src from css. So now how will i use these images of different resolution

现在我有了images inside assets/www/ images文件夹。这个文件夹包含两个相同分辨率的app_logo图像。png和company_logo。png和2张高分辨率的app_logo_big图像。png和company_logo_big。png现在通过媒体查询,我将知道屏幕大小并应用样式,但据我所知,我不能从css中更改img src。现在我要如何使用这些不同分辨率的图像

5 个解决方案

#1


28  

Then Dynamically Change Image through jquery:

然后通过jquery动态改变图像:

HTML:

HTML:


Javascript:

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');  
  }
}

Through CSS: Use Media Queries for Different Resolution :

通过CSS:使用不同分辨率的媒体查询:

HTML:

HTML:


    

CSS:

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); }
}

If you want to filter through,

如果你想过滤,

ORIENTATION - and (orientation: landscape)

方向-及(方向:景观)

Device WIDTH and (min-device-width : 480px) and (max-device-width : 854px)

设备宽度和(min- dedevice宽度:480px)和(max-设备宽度:854px)

Example:

例子:

@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 */
}

#2


3  

You can also do this using a handlebars helper, which less code intensive and in my opinion the recommended method:

你也可以使用一个把手帮助,这是一个较少的代码密集,在我看来推荐的方法:

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


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

while img/low/ and img/high contain images in different resolutions with the same name.

img/low/和img/high在不同分辨率下的图像都有相同的名称。

Then in your template:

然后在你的模板:


Of course, you have to set the screen size values according to the devices your app targets.

当然,你必须根据你的应用程序的目标设置屏幕大小的值。

Appendix: If you do not have 1:1 pixel mapping in cordova browser (which is NOT recommended - your images will have a blurry/unsharp look) screen.width will differ from browsers width (window.innerWidth) and you have to use $(window).width() or window.innerWidth. You might be able to fix a wrong mapping using media queries.

附录:如果你在cordova浏览器中没有1:1的像素映射(这是不推荐的-你的图像会有模糊/不清晰的外观)。宽度不同于浏览器的宽度(window. innerwidth),您必须使用$(window).width()或window. innerwidth。您可能可以使用媒体查询修复错误的映射。

#3


3  

Creating support for more sizes is a problem, but you can fix it with @media queries in CSS. Check this example code:

为更大的尺寸创建支持是一个问题,但是您可以使用CSS中的@media查询来修复它。检查这个示例代码:

/* 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 */
}

With this code you can add support for all devices. Check this link for getting more code for more browsers

使用此代码,您可以添加对所有设备的支持。检查这个链接以获得更多浏览器的代码

Functions which you can use:

你可使用的功能:

  • Width and height: (min-device-width : 768px) and (max-device-width : 1024px)
  • 宽度和高度:(最小设备宽度:768px)和(max-设备宽度:1024px)
  • Orientation: (orientation: landscape) or (orientation: portrait)
  • 方向:(方向:风景)或(方向:肖像)
  • Device pixel ratio: (-webkit-device-pixel-ratio: 1.5)
  • 设备像素比:(-webkit-设备像素比:1.5)

EDIT:

编辑:

HTML:

HTML:


Change img into span and add IDs.

将img更改为span并添加id。

CSS:

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);
  }
}

With this example you can change your code and fix it. Hope this help!

通过这个例子,您可以修改代码并修复它。希望这次的帮助!

#4


1  

I have found I've had to start adding support for pixel ratios of 0.5, 1, 1.3, 1.5, 2 and 3 using these media queries.

我发现我必须开始使用这些媒体查询来增加对0.5、1、1.3、1.5、2和3像素的支持。

Note I am using -webkit-min-device-pixel-ratio rather than -webkit-device-pixel-ratio. I had found that on one of my test devices (Galaxy Tab 3 - 8") the pixel ratio was 1.3 and wasn't picking up any of the specific styles I had set in my phonegap app.

注意,我使用的是-webkit-min-设备像素比,而不是-webkit- de- 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/bigstart@2x.png') 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/bigstart@2x.png') 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/bigstart@2x.png') 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/bigstart@3x.png') no-repeat center bottom;
        background-size: 64px 64px;
    }   
}

#5


0  

I think you have to divide the reported screen dimensions by the screen density to get the media query width and height dimensions.

我认为必须将报告的屏幕尺寸除以屏幕密度,才能得到媒体查询的宽度和高度。


推荐阅读
author-avatar
来人把老师拖出I去毙了
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有