uniapp获取dpi

uniapp获取dpi,第1张

在uniapp中,可以通过uni.getSystemInfoSync() 方法获取设备的屏幕像素密度(DPI)信息,具体步骤如下所示:

1.调用uni.getSystemInfoSync() 方法

在uniapp的页面中,调用uni.getSystemInfoSync() 方法可以返回当前设备的系统信息,包括 *** 作系统版本、手机型号、屏幕分辨率、DPI等参数。

2.解析返回结果并获取DPI值

通过getSystemInfoSync() 方法返回的结果为JSON对象,可以使用其中的“windowWidth”和“screenWidth”字段计算出DPI值。具体方法为:DPI = windowWidth / screenWidth * 160,其中160是安卓平台的标准DPI值。

示例代码如下:

```

const systemInfo = uni.getSystemInfoSync()

const screenWidth = systemInfo.screenWidth

const windowWidth = systemInfo.windowWidth

const dpi = windowWidth / screenWidth * 160

console.log('当前设备的DPI值为:' + dpi)

```

需要注意的是,getSystemInfoSync() 方法只能在小程序或H5平台上使用,在APP平台上需要使用其它的插件或API获取设备信息。另外,DPI值是一个相对值,不同设备的DPI可能会有所区别,因此在UI设计和布局时需要考虑多种分辨率和DPI情况,以保证应用的兼容性和视觉效果。

<template>

    <view class="navbar">

        <view class="navbar-fixed">

            <!-- 状态栏小程序撑起高度 -->

            <view :style="{height:statusBarHeight+'px'}"></view>

            <view class="navbar-content" :style="{height:navBarHeight+'px',width:windowWidth+'px'}">

                <view class="navbar-search">

                    <view class="navbar-search_icon">

                        <uni-icons type="search" size="16" color="#999"></uni-icons>

                    </view>

                    <view class="navbar-serach">

                        <input class="navbar-search_text" type="text" v-model="val" placeholder="请输入您要搜索的内容" />

                    </view>

                </view>

            </view>

        </view>

        <!-- 需要添加占位符高度  状态栏高度+导航栏高度(否则下面tab会塌陷)-->

        <view :style="{height: statusBarHeight+navBarHeight+'px'}"></view>

    </view>

</template>

<script>

    export default {

        name: 'navbar',

        data() {

            return {

                statusBarHeight: 20,/* 状态栏高度 */

                navBarHeight: 45,/* 导航栏高度 */

                windowWidth: 375,/* 窗口宽度 */

                /* 设定状态栏默认高度 */

                val: ''/* 导航栏搜索框的值 */

            }

        },

        created() {

            // 获取手机系统信息

            const info = uni.getSystemInfoSync()

            // 设置状态栏高度(H5顶部无状态栏小程序有状态栏需要撑起高度)

            this.statusBarHeight = info.statusBarHeight

            this.windowWidth = info.windowWidth

            // 除了h5 app mp-alipay的情况下执行

            // #ifndef H5 || APP-PLUS || MP-ALIPAY

            // 获取胶囊的位置

            const menuButtonInfo = uni.getMenuButtonBoundingClientRect()

            console.log(menuButtonInfo)

            // (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏内的高度) = 导航栏的高度

            this.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info.statusBarHeight)

            this.windowWidth = menuButtonInfo.left

            // #endif

        }

    }

</script>

<style lang="less">

    @import './../../uni.less'

    .navbar {

        .navbar-fixed {

            position: fixed

            top: 0

            left: 0

            z-index: 99

            width: 100%

            background-color: @mk-base-color

            .navbar-content {

                display: flex

                justify-content: center

                align-items: center

                padding: 0 15px

                height: 45px

                box-sizing: border-box

                .navbar-search {

                    display: flex

                    align-items: center

                    padding: 0 10px

                    width: 100%

                    height: 30px

                    border-radius: 30px

                    background-color: #fff

                    .navbar-search_icon {

                        // width: 10px

                        // height: 10px

                        margin-right: 10px

                    }

                    .navbar-search_text {

                        width: 100%

                        font-size: 14px

                        color: #999

                    }

                }

                &.search {

                    padding-left: 0

                    .navbar-content__search-icons {

                        margin-left: 10px

                        margin-right: 10px

                    }

                    .navbar-search {

                        border-radius: 5px

                    }

                }

            }

        }

    }

</style>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11340821.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-15
下一篇2023-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存