HarmonyOS 鸿蒙Next学习笔记——设置桌面/锁屏壁纸

HarmonyOS 鸿蒙Next学习笔记——设置桌面/锁屏壁纸

1、常用接口

wallpaperType:壁纸类型

2、权限

允许程序设置静态壁纸
ohos.permission.SET_WALLPAPER
允许程序通过指定显示大小或边距来设置壁纸大小
ohos.permission.SET_WALLPAPER_DIMENSION

3、开发步骤

在config.json中添加权限

"reqPermissions": [
  {
    "name": "ohos.permission.SET_WALLPAPER"
  }
]

ability_main.xml

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">
    <Button
        ohos:id="$+id:btn_set_wallpaper"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="设置桌面壁纸"
        ohos:text_size="40vp"/>
    <Button
        ohos:id="$+id:btn_reset_wallpaper"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="恢复桌面壁纸"
        ohos:text_size="40vp"/>
    <Button
        ohos:id="$+id:btn_set_lock_wallpaper"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="设置锁屏壁纸"
        ohos:text_size="40vp"/>
    <Button
        ohos:id="$+id:btn_reset_lock_wallpaper"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="恢复锁屏壁纸"
        ohos:text_size="40vp"/>
</DirectionalLayout>

效果如下:

设置桌面壁纸

恢复桌面壁纸

设置锁屏壁纸

恢复锁屏壁纸没有成功,不知道是为什么

2 回复

HarmonyOS 鸿蒙Next中,设置桌面和锁屏壁纸可以通过系统提供的API实现。开发者可以使用ohos.wallpaper模块中的WallpaperManager类来管理壁纸。具体步骤如下:

  1. 获取WallpaperManager实例: 使用WallpaperManager.getInstance(context)获取WallpaperManager的实例。

  2. 设置桌面壁纸: 调用setWallpaper(uri, wallpaperType)方法,其中uri为壁纸资源的URI,wallpaperType设置为WallpaperManager.WALLPAPER_SYSTEM

  3. 设置锁屏壁纸: 同样调用setWallpaper(uri, wallpaperType)方法,但wallpaperType设置为WallpaperManager.WALLPAPER_LOCKSCREEN

  4. 获取当前壁纸: 使用getWallpaper(wallpaperType)方法可以获取当前设置的桌面或锁屏壁纸。

示例代码:

import wallpaper from '@ohos.wallpaper';

let context = ... // 获取上下文
let wallpaperManager = wallpaper.getWallpaperManager(context);

// 设置桌面壁纸
let systemWallpaperUri = ... // 桌面壁纸URI
wallpaperManager.setWallpaper(systemWallpaperUri, wallpaper.WallpaperType.WALLPAPER_SYSTEM);

// 设置锁屏壁纸
let lockScreenWallpaperUri = ... // 锁屏壁纸URI
wallpaperManager.setWallpaper(lockScreenWallpaperUri, wallpaper.WallpaperType.WALLPAPER_LOCKSCREEN);

// 获取当前桌面壁纸
let currentSystemWallpaper = wallpaperManager.getWallpaper(wallpaper.WallpaperType.WALLPAPER_SYSTEM);

// 获取当前锁屏壁纸
let currentLockScreenWallpaper = wallpaperManager.getWallpaper(wallpaper.WallpaperType.WALLPAPER_LOCKSCREEN);

以上代码展示了如何在HarmonyOS 鸿蒙Next中设置和获取桌面及锁屏壁纸。

更多关于HarmonyOS 鸿蒙Next学习笔记——设置桌面/锁屏壁纸的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙Next)中设置桌面和锁屏壁纸非常简单。首先,进入“设置”应用,选择“显示与亮度”选项。在“壁纸”部分,你可以选择“桌面壁纸”或“锁屏壁纸”。系统提供了多种预设壁纸,你也可以从相册中选择自定义图片。选择好图片后,调整其显示区域,然后点击“应用”即可。此外,你还可以选择将同一张图片同时设置为桌面和锁屏壁纸,或者分别设置不同的图片。整个过程直观易操作,用户可以根据个人喜好轻松定制设备外观。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!