前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >​OpenHarmony像素单位入门

​OpenHarmony像素单位入门

作者头像
徐建国
发布2022-12-05 15:12:29
1K0
发布2022-12-05 15:12:29
举报
文章被收录于专栏:个人路线

作者:坚果 公众号:"大前端之旅" 华为云享专家,InfoQ签约作者,OpenHarmony布道师,CSDN博客专家,电子发烧友鸿蒙MVP,阿里云专家博主,51CTO博客首席体验官,开源项目GVA成员之一,专注于大前端技术的分享,包括Flutter,小程序,安卓,VUE,JavaScript。

ArkUI开发框架提供了 4 种像素单位供开发者使用,分别是: pxvpfplpx ,框架采用vp为基准数据单位。它们之间的区别如下表所示:

名称

描述

px

屏幕物理像素单位。

vp

屏幕密度相关像素单位,根据屏幕像素密度转换为屏幕物理像素。

fp

字体像素,与vp类似适用于屏幕密度变化,随系统字体大小设置变化。

lpx

视窗逻辑像素单位,lpx单位为实际屏幕宽度与逻辑宽度(在 config.json 中配置的 designWidth )的比值,如配置 designWdith 为 720 时,在实际宽度为 1440 物理像素的屏幕上, 1px 为 2px 。

ArkUI开发框架也提供了全局方法把这些不同的尺寸单位相互转换,全局方法如下所示:

代码语言:javascript
复制
declare function vp2px(value: number): number;declare function px2vp(value: number): number;declare function fp2px(value: number): number;declare function px2fp(value: number): number;declare function lpx2px(value: number): number;declare function px2lpx(value: number): number;

像素单位转换

提供其他单位与px单位互相转换的方法。

接口

描述

vp2px(value : number) : number

将vp单位的数值转换为以px为单位的数值。

px2vp(value : number) : number

将px单位的数值转换为以vp为单位的数值。

fp2px(value : number) : number

将fp单位的数值转换为以px为单位的数值。

px2fp(value : number) : number

将px单位的数值转换为以fp为单位的数值。

lpx2px(value : number) : number

将lpx单位的数值转换为以px为单位的数值。

px2lpx(value : number) : number

将px单位的数值转换为以lpx为单位的数值。

最后我们通过代码来体验一下

代码语言:javascript
复制
/*
 * Copyright (c) 2021 JianGuo Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */@Entry@Componentstruct Example {
  build() {
    Column() {

        Column() {
          Text("width(220)")
            .width(220).height(40).backgroundColor(0xF9CF93)
            .textAlign(TextAlign.Center).fontColor(Color.White).fontSize('12vp')
        }.margin(5)
        Column() {
          Text("width('220px')")
            .width('220px').height(40).backgroundColor(0xF9CF93)
            .textAlign(TextAlign.Center).fontColor(Color.White)
        }.margin(5)
        Column() {
          Text("width('220vp')")
            .width('220vp').height(40).backgroundColor(0xF9CF93)
            .textAlign(TextAlign.Center).fontColor(Color.White).fontSize('12vp')
        }.margin(5)
        Column() {
          Text("width('220lpx') designWidth:720")
            .width('220lpx').height(40).backgroundColor(0xF9CF93)
            .textAlign(TextAlign.Center).fontColor(Color.White).fontSize('12vp')
        }.margin(5)
        Column() {
          Text("width(vp2px(220) + 'px')")
            .width(vp2px(220) + 'px').height(40).backgroundColor(0xF9CF93)
            .textAlign(TextAlign.Center).fontColor(Color.White).fontSize('12vp')
        }.margin(5)
        Column() {
          Text("fontSize('12fp')")
            .width(220).height(40).backgroundColor(0xF9CF93)
            .textAlign(TextAlign.Center).fontColor(Color.White).fontSize('12fp')
        }.margin(5)
      }.width('100%').height("100%").justifyContent(FlexAlign.Center)
    }

}

参考文档

像素单位

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-08-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大前端之旅 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 像素单位转换
  • 参考文档
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档