Android中HTML资源中不同分辨率的不同图像

发布于 2024-10-02 17:45:50 字数 161 浏览 1 评论 0原文

如何在 Android 应用程序的嵌入式 HTML 资源中针对不同的屏幕分辨率(hdpi、ldpi、mdpi)显示不同的图像?

HTML 资源位于 assets 中,但可以根据需要进行更改。

如果这是不可能的,如何根据屏幕分辨率更改图像的显示尺寸?

How do you display different images for different screen resolutions (hdpi, ldpi, mdpi) in an embedded HTML resource of an Android app?

The HTML resource is located in assets, but can be changed if necessary.

If this is not possible, how do you change the display size of the image based on the screen resolution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最舍不得你 2024-10-09 17:45:50
/*
MDPI Resources for Midium-density (Mdpi) screens (~160dpi).
CSS for medium density (Mdpi) Android layouts in here
webkit-device-pixel-ratio:1
*/
@media only screen and (-webkit-min-device-pixel-ratio: 1),
       only screen and (min--moz-device-pixel-ratio: 1),
       only screen and (min-resolution: 160dpi)
{
    .title
    {
      text-align: center; 
      vertical-align: middle; 
      color: White; 
      font-size: 1.25em; 
    }
}


/* 
High Density Resources for (Hdpi) screens (~240dpi).
CSS for High density (Hdpi) Android layouts in here
webkit-device-pixel-ratio:1.5
*/
@media only screen and (-webkit-min-device-pixel-ratio: 1.5),
       only screen and (min--moz-device-pixel-ratio: 1.5),
       only screen and (min-resolution: 240dpi)
{
    .title
    {
      text-align: center; 
      vertical-align: middle; 
      color: White; 
      font-size: 2.25em; 
    }
}
/*
MDPI Resources for Midium-density (Mdpi) screens (~160dpi).
CSS for medium density (Mdpi) Android layouts in here
webkit-device-pixel-ratio:1
*/
@media only screen and (-webkit-min-device-pixel-ratio: 1),
       only screen and (min--moz-device-pixel-ratio: 1),
       only screen and (min-resolution: 160dpi)
{
    .title
    {
      text-align: center; 
      vertical-align: middle; 
      color: White; 
      font-size: 1.25em; 
    }
}


/* 
High Density Resources for (Hdpi) screens (~240dpi).
CSS for High density (Hdpi) Android layouts in here
webkit-device-pixel-ratio:1.5
*/
@media only screen and (-webkit-min-device-pixel-ratio: 1.5),
       only screen and (min--moz-device-pixel-ratio: 1.5),
       only screen and (min-resolution: 240dpi)
{
    .title
    {
      text-align: center; 
      vertical-align: middle; 
      color: White; 
      font-size: 2.25em; 
    }
}
樱娆 2024-10-09 17:45:50

我设法使用 @media 查询和 -webkit-device-pixel-ratio 来做到这一点。虽然图像必须位于资产中,但您可以使用文件夹来正确组织它们。

@media screen and (-webkit-device-pixel-ratio:0.75) {
    #header {
        background-image: url('ldpi/background.png');
        width: 75px;
        height: 75px;
    }
}

@media screen and (-webkit-device-pixel-ratio:1.0) {
    #header {
        background-image: url('mdpi/background.png');
        width: 100px;
        height: 100px;
    }
}

@media screen and (-webkit-device-pixel-ratio:1.5) {
    #header {
        background-image: url('hdpi/background.png');
        width: 150px;
        height: 150px;
    }
}

根据文档构建网页以支持不同的屏幕密度),这仅适用于 Android 2.0 或更高版本。

看来你对 CSS 的了解永远都不够。

I managed to do this with @media queries and -webkit-device-pixel-ratio. While the images have to be located in assets, you can use folders to organize them properly.

@media screen and (-webkit-device-pixel-ratio:0.75) {
    #header {
        background-image: url('ldpi/background.png');
        width: 75px;
        height: 75px;
    }
}

@media screen and (-webkit-device-pixel-ratio:1.0) {
    #header {
        background-image: url('mdpi/background.png');
        width: 100px;
        height: 100px;
    }
}

@media screen and (-webkit-device-pixel-ratio:1.5) {
    #header {
        background-image: url('hdpi/background.png');
        width: 150px;
        height: 150px;
    }
}

According to the documentation (Building web pages to support different screen densities), this only works in Android 2.0 or higher.

You never know enough CSS, it seems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文