OpenHarmony像素單位
ArkUI開發(fā)框架提供了 4 種像素單位供開發(fā)者使用,分別是: px
、 vp
、 fp
和 lpx
,框架采用vp為基準(zhǔn)數(shù)據(jù)單位。它們之間的區(qū)別如下表所示:
名稱 | 描述 |
---|---|
px | 屏幕物理像素單位。 |
vp | 屏幕密度相關(guān)像素單位,根據(jù)屏幕像素密度轉(zhuǎn)換為屏幕物理像素。 |
fp | 字體像素,與vp類似適用于屏幕密度變化,隨系統(tǒng)字體大小設(shè)置變化。 |
lpx | 視窗邏輯像素單位,lpx單位為實(shí)際屏幕寬度與邏輯寬度(在 config.json 中配置的 designWidth )的比值,如配置 designWdith 為 720 時(shí),在實(shí)際寬度為 1440 物理像素的屏幕上, 1px 為 2px 。 |
ArkUI開發(fā)框架也提供了全局方法把這些不同的尺寸單位相互轉(zhuǎn)換,全局方法如下所示:
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
;
像素單位轉(zhuǎn)換
提供其他單位與px單位互相轉(zhuǎn)換的方法。
接口 | 描述 |
---|---|
vp2px(value : number) : number | 將vp單位的數(shù)值轉(zhuǎn)換為以px為單位的數(shù)值。 |
px2vp(value : number) : number | 將px單位的數(shù)值轉(zhuǎn)換為以vp為單位的數(shù)值。 |
fp2px(value : number) : number | 將fp單位的數(shù)值轉(zhuǎn)換為以px為單位的數(shù)值。 |
px2fp(value : number) : number | 將px單位的數(shù)值轉(zhuǎn)換為以fp為單位的數(shù)值。 |
lpx2px(value : number) : number | 將lpx單位的數(shù)值轉(zhuǎn)換為以px為單位的數(shù)值。 |
px2lpx(value : number) : number | 將px單位的數(shù)值轉(zhuǎn)換為以lpx為單位的數(shù)值。 |
最后我們通過代碼來體驗(yàn)一下
/*
* 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
@
Component
struct
Example
{
build
() {
Column
() {
?
Column
() {
Text
(
"width(220)"
)
.
width
(
220
).
height
(
40
).
(
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
)
}
?
}
?
![image-20220805133455311](https://file.elecfans.com//web2/M00/66/AF/pYYBAGMOqfmAdjLgAACRMjk28bQ178.png)
參考文檔
像素單位
-
像素
+關(guān)注
關(guān)注
1文章
205瀏覽量
18632 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3747瀏覽量
16589
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
鴻蒙基礎(chǔ)開發(fā)實(shí)戰(zhàn)-(ArkTS)像素轉(zhuǎn)換
開源的 OpenHarmony 是每個(gè)人的 OpenHarmony
openharmony開源項(xiàng)目
openharmony發(fā)起單位
OpenHarmony生態(tài)領(lǐng)航貢獻(xiàn)單位公布
OpenHarmony開發(fā)者大會(huì)2023生態(tài)示范單位及OpenHarmony生態(tài)委員會(huì)成員單位致謝授牌:方案評(píng)定辦法公示
OpenHarmony開發(fā)者大會(huì)2023杰出貢獻(xiàn)單位致謝授牌評(píng)選規(guī)則
OpenHarmony開發(fā)者大會(huì)舉辦,OpenHarmony項(xiàng)目群授牌30家捐贈(zèng)單位及個(gè)人
![<b class='flag-5'>OpenHarmony</b>開發(fā)者大會(huì)舉辦,<b class='flag-5'>OpenHarmony</b>項(xiàng)目群授牌30家捐贈(zèng)<b class='flag-5'>單位</b>及個(gè)人](https://file1.elecfans.com/web2/M00/82/25/wKgZomRE4JyAVvrnAABbIKCy468970.png)
OpenHarmony開發(fā)者大會(huì)2023召開,致謝六家百人代碼貢獻(xiàn)單位
九聯(lián)科技獲得授牌“OpenHarmony安全委員會(huì)委員單位”
億晟科技被評(píng)為OpenHarmony生態(tài)開發(fā)板貢獻(xiàn)單位
![億晟科技被評(píng)為<b class='flag-5'>OpenHarmony</b>生態(tài)開發(fā)板貢獻(xiàn)<b class='flag-5'>單位</b>](https://file.elecfans.com/web2/M00/13/01/pYYBAGEvNtmAJi2NAABwHspjfHc418.png)
潤(rùn)和軟件受聘擔(dān)任OpenHarmony安全委員會(huì)副主席單位
![潤(rùn)和軟件受聘擔(dān)任<b class='flag-5'>OpenHarmony</b>安全委員會(huì)副主席<b class='flag-5'>單位</b>](https://file1.elecfans.com/web2/M00/8C/DA/wKgZomSzq7iAfLLBAAAa6cfxe34057.jpg)
評(píng)論