提交 df28197a 编写于 作者: W weishaoxiong

fixed 9eb15e93 from https://gitee.com/weishaoxiong/docs/pulls/12360

Signed-off-by: Nweishaoxiong <weishaoxiong1@huawei.com>
fix: 示例代码修改 及 callback描述修改
Signed-off-by: Nweishaoxiong <weishaoxiong1@huawei.com>
fix:【wallpaper】API参考优化
上级 62af10e7
...@@ -26,15 +26,51 @@ import wallpaper from '@ohos.wallpaper'; ...@@ -26,15 +26,51 @@ import wallpaper from '@ohos.wallpaper';
| WALLPAPER_LOCKSCREEN | 1 |锁屏壁纸标识。 | | WALLPAPER_LOCKSCREEN | 1 |锁屏壁纸标识。 |
## wallpaper.getColors ## RgbaColor
getColors(wallpaperType: WallpaperType, callback: AsyncCallback&lt;Array&lt;RgbaColor&gt;&gt;): void 定义壁纸颜色信息结构。
**系统能力**: SystemCapability.MiscServices.Wallpaper
| 名称 | 类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
| red | number | 是 | 是 | 表示红色值,范围为 0 到 255。 |
| green | number | 是 | 是 | 表示绿色值,范围为 0 到 255。 |
| blue | number | 是 | 是 | 表示蓝色值,范围为 0 到 255。 |
| alpha | number | 是 | 是 | 表示 alpha 值,范围为 0 到 255。 |
## wallpaper.getColorsSync<sup>9+</sup>
getColorsSync(wallpaperType: WallpaperType): Array&lt;RgbaColor&gt;
获取指定类型壁纸的主要颜色信息。 获取指定类型壁纸的主要颜色信息。
> **说明:** **系统能力**: SystemCapability.MiscServices.Wallpaper
>
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getColorsSync<sup>9+</sup>](#wallpapergetcolorssync9)代替。 **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
**返回值**
| 类型 | 说明 |
| -------- | -------- |
| Array&lt;[RgbaColor](#rgbacolor)&gt; | 返回壁纸的主要颜色信息。 |
**示例**
```js
let colors = wallpaper.getColorsSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM);
```
## wallpaper.getIdSync<sup>9+</sup>
getIdSync(wallpaperType: WallpaperType): number
获取指定类型壁纸的ID。
**系统能力**: SystemCapability.MiscServices.Wallpaper **系统能力**: SystemCapability.MiscServices.Wallpaper
...@@ -43,29 +79,135 @@ getColors(wallpaperType: WallpaperType, callback: AsyncCallback&lt;Array&lt;Rgba ...@@ -43,29 +79,135 @@ getColors(wallpaperType: WallpaperType, callback: AsyncCallback&lt;Array&lt;Rgba
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | | wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;Array&lt;[RgbaColor](#rgbacolor)&gt;&gt; | 是 | 回调函数,返回壁纸的主要颜色信息。 |
**返回值**
| 类型 | 说明 |
| -------- | -------- |
| number | 返回壁纸的ID。如果配置了这种壁纸类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1~2^31-1。 |
**示例**
```js
let id = wallpaper.getIdSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM);
```
## wallpaper.getMinHeightSync<sup>9+</sup>
getMinHeightSync(): number
获取壁纸的最小高度值。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| number | 返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 |
**示例:** **示例:**
```js ```js
wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => { let minHeight = wallpaper.getMinHeightSync();
```
## wallpaper.getMinWidthSync<sup>9+</sup>
getMinWidthSync(): number
获取壁纸的最小宽度值。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| number | 壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 |
**示例:**
```js
let minWidth = wallpaper.getMinWidthSync();
```
## wallpaper.isChangeAllowed<sup>9+</sup>
isChangeAllowed(): boolean
是否允许应用改变当前用户的壁纸。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| boolean | 返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 |
**示例:**
```js
let isChangeAllowed = wallpaper.isChangeAllowed();
```
## wallpaper.isUserChangeAllowed<sup>9+</sup>
isUserChangeAllowed(): boolean
是否允许用户设置壁纸。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| boolean | 返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 |
**示例:**
```js
let isUserChangeAllowed = wallpaper.isUserChangeAllowed();
```
## wallpaper.restore<sup>9+</sup>
restore(wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
移除指定类型的壁纸,恢复为默认显示的壁纸。
**需要权限**:ohos.permission.SET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,移除壁纸成功,error为undefined 否则返回error信息。 |
**示例:**
```js
wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
if (error) { if (error) {
console.error(`failed to getColors because: ` + JSON.stringify(error)); console.error(`failed to restore because: ${JSON.stringify(error)}`);
return; return;
} }
console.log(`success to getColors.`); console.log(`success to restore.`);
}); });
``` ```
## wallpaper.getColors ## wallpaper.restore<sup>9+</sup>
getColors(wallpaperType: WallpaperType): Promise&lt;Array&lt;RgbaColor&gt;&gt; restore(wallpaperType: WallpaperType): Promise&lt;void&gt;
获取指定类型壁纸的主要颜色信息 移除指定类型的壁纸,恢复为默认显示的壁纸
> **说明:** **需要权限**:ohos.permission.SET_WALLPAPER
>
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getColorsSync<sup>9+</sup>](#wallpapergetcolorssync9)代替。
**系统能力**: SystemCapability.MiscServices.Wallpaper **系统能力**: SystemCapability.MiscServices.Wallpaper
...@@ -79,24 +221,312 @@ getColors(wallpaperType: WallpaperType): Promise&lt;Array&lt;RgbaColor&gt;&gt; ...@@ -79,24 +221,312 @@ getColors(wallpaperType: WallpaperType): Promise&lt;Array&lt;RgbaColor&gt;&gt;
| 类型 | 说明 | | 类型 | 说明 |
| -------- | -------- | | -------- | -------- |
| Promise&lt;Array&lt;[RgbaColor](#rgbacolor)&gt;&gt; | 返回壁纸的主要颜色信息。 | | Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
**示例:** **示例:**
```js
wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
console.log(`success to restore.`);
}).catch((error) => {
console.error(`failed to restore because: ${JSON.stringify(error)}`);
});
```
## wallpaper.setImage<sup>9+</sup>
setImage(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
将指定资源设置为指定类型的壁纸。
**需要权限**:ohos.permission.SET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,设置壁纸成功,error为undefined 否则返回error信息。 |
**示例:**
```js
// source类型为string
let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
if (error) {
console.error(`failed to setImage because: ${JSON.stringify(error)}`);
return;
}
console.log(`success to setImage.`);
});
// source类型为image.PixelMap
import image from '@ohos.multimedia.image';
let imageSource = image.createImageSource("file://" + wallpaperPath);
let opts = {
"desiredSize": {
"height": 3648,
"width": 2736
}
};
imageSource.createPixelMap(opts).then((pixelMap) => {
wallpaper.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
if (error) {
console.error(`failed to setImage because: ${JSON.stringify(error)}`);
return;
}
console.log(`success to setImage.`);
});
}).catch((error) => {
console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
});
```
## wallpaper.setImage<sup>9+</sup>
setImage(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise&lt;void&gt;
将指定资源设置为指定类型的壁纸。
**需要权限**:ohos.permission.SET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
**示例:**
```js ```js
wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => { // source类型为string
console.log(`success to getColors.`); let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
console.log(`success to setImage.`);
}).catch((error) => {
console.error(`failed to setImage because: ${JSON.stringify(error)}`);
});
// source类型为image.PixelMap
import image from '@ohos.multimedia.image';
let imageSource = image.createImageSource("file://" + wallpaperPath);
let opts = {
"desiredSize": {
"height": 3648,
"width": 2736
}
};
imageSource.createPixelMap(opts).then((pixelMap) => {
wallpaper.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
console.log(`success to setImage.`);
}).catch((error) => {
console.error(`failed to setImage because: ${JSON.stringify(error)}`);
});
}).catch((error) => {
console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
});
```
## wallpaper.getFileSync<sup>9+</sup>
getFileSync(wallpaperType: WallpaperType): number;
获取指定类型的壁纸文件。
**需要权限**:ohos.permission.GET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| number | 调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 |
**示例:**
```js
let file = wallpaper.getFileSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM);
```
## wallpaper.getImage<sup>9+</sup>
getImage(wallpaperType: WallpaperType, callback: AsyncCallback&lt;image.PixelMap&gt;): void;
获取壁纸图片的像素图。
**需要权限**:ohos.permission.GET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**系统接口**:此接口为系统接口。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | 是 | 回调函数,调用成功则返回壁纸图片的像素图大小,调用失败则返回error信息。 |
**示例:**
```js
wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM, function (error, data) {
if (error) {
console.error(`failed to getImage because: ${JSON.stringify(error)}`);
return;
}
console.log(`success to getImage: ${JSON.stringify(data)}`);
});
```
## wallpaper.getImage<sup>9+</sup>
getImage(wallpaperType: WallpaperType): Promise&lt;image.PixelMap&gt;
获取壁纸图片的像素图。
**需要权限**:ohos.permission.GET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**系统接口**:此接口为系统接口。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | 调用成功则返回壁纸图片的像素图大小,调用失败则返回error信息。 |
**示例:**
```js
wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
console.log(`success to getImage: ${JSON.stringify(data)}`);
}).catch((error) => { }).catch((error) => {
console.error(`failed to getColors because: ` + JSON.stringify(error)); console.error(`failed to getImage because: ${JSON.stringify(error)}`);
}); });
``` ```
## wallpaper.getColorsSync<sup>9+</sup> ## wallpaper.on('colorChange')<sup>9+</sup>
getColorsSync(wallpaperType: WallpaperType): Array&lt;RgbaColor&gt; on(type: 'colorChange', callback: (colors: Array&lt;RgbaColor&gt;, wallpaperType: WallpaperType) =&gt; void): void
订阅壁纸颜色变化结果上报事件。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| type | string | 是 | 取值为'colorChange',表示壁纸颜色变化结果上报事件。 |
| callback | function | 是 | 壁纸颜色变化触发该回调方法,返回壁纸类型和壁纸的主要颜色信息。<br/>- colors<br/> 壁纸的主要颜色信息,其类型见[RgbaColor](#rgbacolor)<br/>- wallpaperType<br/> 壁纸类型。 |
**示例:**
```js
let listener = (colors, wallpaperType) => {
console.log(`wallpaper color changed.`);
};
wallpaper.on('colorChange', listener);
```
## wallpaper.off('colorChange')<sup>9+</sup>
off(type: 'colorChange', callback?: (colors: Array&lt;RgbaColor&gt;, wallpaperType: WallpaperType) =&gt; void): void
取消订阅壁纸颜色变化结果上报事件。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| type | string | 是 | 取值为'colorChange',表示取消订阅壁纸颜色变化结果上报事件。 |
| callback | function | 否 | 表示要取消的壁纸颜色变化的回调,不填写该参数则取消订阅该type对应的所有回调。<br/>- colors<br/> 壁纸的主要颜色信息,其类型见[RgbaColor](#rgbacolor)<br/>- wallpaperType<br/> 壁纸类型。 |
**示例:**
```js
let listener = (colors, wallpaperType) => {
console.log(`wallpaper color changed.`);
};
wallpaper.on('colorChange', listener);
// 取消订阅listener
wallpaper.off('colorChange', listener);
// 取消所有'colorChange'类型的订阅
wallpaper.off('colorChange');
```
## wallpaper.getColors<sup>(deprecated)</sup>
getColors(wallpaperType: WallpaperType, callback: AsyncCallback&lt;Array&lt;RgbaColor&gt;&gt;): void
获取指定类型壁纸的主要颜色信息。
> **说明:**
>
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getColorsSync<sup>9+</sup>](#wallpapergetcolorssync9)代替。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;Array&lt;[RgbaColor](#rgbacolor)&gt;&gt; | 是 | 回调函数,返回壁纸的主要颜色信息。 |
**示例:**
```js
wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
if (error) {
console.error(`failed to getColors because: ${JSON.stringify(error)}`);
return;
}
console.log(`success to getColors: ${JSON.stringify(data)}`);
});
```
## wallpaper.getColors<sup>(deprecated)</sup>
getColors(wallpaperType: WallpaperType): Promise&lt;Array&lt;RgbaColor&gt;&gt;
获取指定类型壁纸的主要颜色信息。 获取指定类型壁纸的主要颜色信息。
> **说明:**
>
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getColorsSync<sup>9+</sup>](#wallpapergetcolorssync9)代替。
**系统能力**: SystemCapability.MiscServices.Wallpaper **系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:** **参数:**
...@@ -105,19 +535,23 @@ getColorsSync(wallpaperType: WallpaperType): Array&lt;RgbaColor&gt; ...@@ -105,19 +535,23 @@ getColorsSync(wallpaperType: WallpaperType): Array&lt;RgbaColor&gt;
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | | wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
**返回值** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| -------- | -------- | | -------- | -------- |
| Array&lt;[RgbaColor](#rgbacolor)&gt; | 返回壁纸的主要颜色信息。 | | Promise&lt;Array&lt;[RgbaColor](#rgbacolor)&gt;&gt; | 返回壁纸的主要颜色信息。 |
**示例** **示例:**
```js ```js
let colors = wallpaper.getColorsSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM); wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
console.log(`success to getColors: ${JSON.stringify(data)}`);
}).catch((error) => {
console.error(`failed to getColors because: ${JSON.stringify(error)}`);
});
``` ```
## wallpaper.getId ## wallpaper.getId<sup>(deprecated)</sup>
getId(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void getId(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void
...@@ -137,18 +571,18 @@ getId(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void ...@@ -137,18 +571,18 @@ getId(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回壁纸的ID。如果配置了指定类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1~2^31-1。 | | callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回壁纸的ID。如果配置了指定类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1~2^31-1。 |
**示例:** **示例:**
```js ```js
wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => { wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
if (error) { if (error) {
console.error(`failed to getId because: ` + JSON.stringify(error)); console.error(`failed to getId because: ${JSON.stringify(error)}`);
return; return;
} }
console.log(`success to getId: ` + JSON.stringify(data)); console.log(`success to getId: ${JSON.stringify(data)}`);
}); });
``` ```
## wallpaper.getId ## wallpaper.getId<sup>(deprecated)</sup>
getId(wallpaperType: WallpaperType): Promise&lt;number&gt; getId(wallpaperType: WallpaperType): Promise&lt;number&gt;
...@@ -173,42 +607,16 @@ getId(wallpaperType: WallpaperType): Promise&lt;number&gt; ...@@ -173,42 +607,16 @@ getId(wallpaperType: WallpaperType): Promise&lt;number&gt;
| Promise&lt;number&gt; | 壁纸的ID。如果配置了这种壁纸类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1~2^31-1。 | | Promise&lt;number&gt; | 壁纸的ID。如果配置了这种壁纸类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1~2^31-1。 |
**示例:** **示例:**
```js ```js
wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => { wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
console.log(`success to getId: ` + JSON.stringify(data)); console.log(`success to getId: ${JSON.stringify(data)}`);
}).catch((error) => { }).catch((error) => {
console.error(`failed to getId because: ` + JSON.stringify(error)); console.error(`failed to getId because: ${JSON.stringify(error)}`);
}); });
``` ```
## wallpaper.getIdSync<sup>9+</sup> ## wallpaper.getMinHeight<sup>(deprecated)</sup>
getIdSync(wallpaperType: WallpaperType): number
获取指定类型壁纸的ID。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
**返回值**
| 类型 | 说明 |
| -------- | -------- |
| number | 返回壁纸的ID。如果配置了这种壁纸类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1~2^31-1。 |
**示例**
```js
let id = wallpaper.getIdSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM);
```
## wallpaper.getMinHeight
getMinHeight(callback: AsyncCallback&lt;number&gt;): void getMinHeight(callback: AsyncCallback&lt;number&gt;): void
...@@ -227,18 +635,18 @@ getMinHeight(callback: AsyncCallback&lt;number&gt;): void ...@@ -227,18 +635,18 @@ getMinHeight(callback: AsyncCallback&lt;number&gt;): void
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 | | callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 |
**示例:** **示例:**
```js ```js
wallpaper.getMinHeight((error, data) => { wallpaper.getMinHeight((error, data) => {
if (error) { if (error) {
console.error(`failed to getMinHeight because: ` + JSON.stringify(error)); console.error(`failed to getMinHeight because: ${JSON.stringify(error)}`);
return; return;
} }
console.log(`success to getMinHeight: ` + JSON.stringify(data)); console.log(`success to getMinHeight: ${JSON.stringify(data)}`);
}); });
``` ```
## wallpaper.getMinHeight ## wallpaper.getMinHeight<sup>(deprecated)</sup>
getMinHeight(): Promise&lt;number&gt; getMinHeight(): Promise&lt;number&gt;
...@@ -257,36 +665,16 @@ getMinHeight(): Promise&lt;number&gt; ...@@ -257,36 +665,16 @@ getMinHeight(): Promise&lt;number&gt;
| Promise&lt;number&gt; | 返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 | | Promise&lt;number&gt; | 返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 |
**示例:** **示例:**
```js ```js
wallpaper.getMinHeight().then((data) => { wallpaper.getMinHeight().then((data) => {
console.log(`success to getMinHeight: ` + JSON.stringify(data)); console.log(`success to getMinHeight: ${JSON.stringify(data)}`);
}).catch((error) => { }).catch((error) => {
console.error(`failed to getMinHeight because: ` + JSON.stringify(error)); console.error(`failed to getMinHeight because: ${JSON.stringify(error)}`);
}); });
``` ```
## wallpaper.getMinHeightSync<sup>9+</sup> ## wallpaper.getMinWidth<sup>(deprecated)</sup>
getMinHeightSync(): number
获取壁纸的最小高度值。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| number | 返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 |
**示例:**
```js
let minHeight = wallpaper.getMinHeightSync();
```
## wallpaper.getMinWidth
getMinWidth(callback: AsyncCallback&lt;number&gt;): void getMinWidth(callback: AsyncCallback&lt;number&gt;): void
...@@ -305,18 +693,18 @@ getMinWidth(callback: AsyncCallback&lt;number&gt;): void ...@@ -305,18 +693,18 @@ getMinWidth(callback: AsyncCallback&lt;number&gt;): void
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 | | callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 |
**示例:** **示例:**
```js ```js
wallpaper.getMinWidth((error, data) => { wallpaper.getMinWidth((error, data) => {
if (error) { if (error) {
console.error(`failed to getMinWidth because: ` + JSON.stringify(error)); console.error(`failed to getMinWidth because: ${JSON.stringify(error)}`);
return; return;
} }
console.log(`success to getMinWidth: ` + JSON.stringify(data)); console.log(`success to getMinWidth: ${JSON.stringify(data)}`);
}); });
``` ```
## wallpaper.getMinWidth ## wallpaper.getMinWidth<sup>(deprecated)</sup>
getMinWidth(): Promise&lt;number&gt; getMinWidth(): Promise&lt;number&gt;
...@@ -335,36 +723,16 @@ getMinWidth(): Promise&lt;number&gt; ...@@ -335,36 +723,16 @@ getMinWidth(): Promise&lt;number&gt;
| Promise&lt;number&gt; | 壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 | | Promise&lt;number&gt; | 壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 |
**示例:** **示例:**
```js ```js
wallpaper.getMinWidth().then((data) => { wallpaper.getMinWidth().then((data) => {
console.log(`success to getMinWidth: ` + JSON.stringify(data)); console.log(`success to getMinWidth: ${JSON.stringify(data)}`);
}).catch((error) => { }).catch((error) => {
console.error(`failed to getMinWidth because: ` + JSON.stringify(error)); console.error(`failed to getMinWidth because: ${JSON.stringify(error)}`);
}); });
``` ```
## wallpaper.getMinWidthSync<sup>9+</sup> ## wallpaper.isChangePermitted<sup>(deprecated)</sup>
getMinWidthSync(): number
获取壁纸的最小宽度值。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| number | 壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 |
**示例:**
```js
let minWidth = wallpaper.getMinWidthSync();
```
## wallpaper.isChangePermitted
isChangePermitted(callback: AsyncCallback&lt;boolean&gt;): void isChangePermitted(callback: AsyncCallback&lt;boolean&gt;): void
...@@ -383,18 +751,18 @@ isChangePermitted(callback: AsyncCallback&lt;boolean&gt;): void ...@@ -383,18 +751,18 @@ isChangePermitted(callback: AsyncCallback&lt;boolean&gt;): void
| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数,返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 | | callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数,返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 |
**示例:** **示例:**
```js ```js
wallpaper.isChangePermitted((error, data) => { wallpaper.isChangePermitted((error, data) => {
if (error) { if (error) {
console.error(`failed to isChangePermitted because: ` + JSON.stringify(error)); console.error(`failed to isChangePermitted because: ${JSON.stringify(error)}`);
return; return;
} }
console.log(`success to isChangePermitted: ` + JSON.stringify(data)); console.log(`success to isChangePermitted: ${JSON.stringify(data)}`);
}); });
``` ```
## wallpaper.isChangePermitted ## wallpaper.isChangePermitted<sup>(deprecated)</sup>
isChangePermitted(): Promise&lt;boolean&gt; isChangePermitted(): Promise&lt;boolean&gt;
...@@ -413,36 +781,16 @@ isChangePermitted(): Promise&lt;boolean&gt; ...@@ -413,36 +781,16 @@ isChangePermitted(): Promise&lt;boolean&gt;
| Promise&lt;boolean&gt; | 返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 | | Promise&lt;boolean&gt; | 返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 |
**示例:** **示例:**
```js ```js
wallpaper.isChangePermitted().then((data) => { wallpaper.isChangePermitted().then((data) => {
console.log(`success to isChangePermitted: ` + JSON.stringify(data)); console.log(`success to isChangePermitted: ${JSON.stringify(data)}`);
}).catch((error) => { }).catch((error) => {
console.error(`failed to isChangePermitted because: ` + JSON.stringify(error)); console.error(`failed to isChangePermitted because: ${JSON.stringify(error)}`);
}); });
``` ```
## wallpaper.isChangeAllowed<sup>9+</sup> ## wallpaper.isOperationAllowed<sup>(deprecated)</sup>
isChangeAllowed(): boolean
是否允许应用改变当前用户的壁纸。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| boolean | 返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 |
**示例:**
```js
let isChangeAllowed = wallpaper.isChangeAllowed();
```
## wallpaper.isOperationAllowed
isOperationAllowed(callback: AsyncCallback&lt;boolean&gt;): void isOperationAllowed(callback: AsyncCallback&lt;boolean&gt;): void
...@@ -461,18 +809,18 @@ isOperationAllowed(callback: AsyncCallback&lt;boolean&gt;): void ...@@ -461,18 +809,18 @@ isOperationAllowed(callback: AsyncCallback&lt;boolean&gt;): void
| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数,返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 | | callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数,返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 |
**示例:** **示例:**
```js ```js
wallpaper.isOperationAllowed((error, data) => { wallpaper.isOperationAllowed((error, data) => {
if (error) { if (error) {
console.error(`failed to isOperationAllowed because: ` + JSON.stringify(error)); console.error(`failed to isOperationAllowed because: ${JSON.stringify(error)}`);
return; return;
} }
console.log(`success to isOperationAllowed: ` + JSON.stringify(data)); console.log(`success to isOperationAllowed: ${JSON.stringify(data)}`);
}); });
``` ```
## wallpaper.isOperationAllowed ## wallpaper.isOperationAllowed<sup>(deprecated)</sup>
isOperationAllowed(): Promise&lt;boolean&gt; isOperationAllowed(): Promise&lt;boolean&gt;
...@@ -491,174 +839,24 @@ isOperationAllowed(): Promise&lt;boolean&gt; ...@@ -491,174 +839,24 @@ isOperationAllowed(): Promise&lt;boolean&gt;
| Promise&lt;boolean&gt; | 异步回调函数,返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 | | Promise&lt;boolean&gt; | 异步回调函数,返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 |
**示例:** **示例:**
```js ```js
wallpaper.isOperationAllowed().then((data) => { wallpaper.isOperationAllowed().then((data) => {
console.log(`success to isOperationAllowed: ` + JSON.stringify(data)); console.log(`success to isOperationAllowed: ${JSON.stringify(data)}`);
}).catch((error) => {
console.error(`failed to isOperationAllowed because: ` + JSON.stringify(error));
});
```
## wallpaper.isUserChangeAllowed<sup>9+</sup>
isUserChangeAllowed(): boolean
是否允许用户设置壁纸。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| boolean | 返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 |
**示例:**
```js
let isUserChangeAllowed = wallpaper.isUserChangeAllowed();
```
## wallpaper.reset
reset(wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
移除指定类型的壁纸,恢复为默认显示的壁纸。
> **说明:**
>
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.restore<sup>9+</sup>](#wallpaperrestore9)代替。
**需要权限**:ohos.permission.SET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,调用成功则返回是否移除成功的结果,调用失败则返回error信息。 |
**示例:**
```js
wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
if (error) {
console.error(`failed to reset because: ` + JSON.stringify(error));
return;
}
console.log(`success to reset.`);
});
```
## wallpaper.reset
reset(wallpaperType: WallpaperType): Promise&lt;void&gt;
移除指定类型的壁纸,恢复为默认显示的壁纸。
> **说明:**
>
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.restore<sup>9+</sup>](#wallpaperrestore9)代替。
**需要权限**:ohos.permission.SET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 调用成功则返回是否移除成功的结果,调用失败则返回error信息。 |
**示例:**
```js
wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
console.log(`success to reset.`);
}).catch((error) => {
console.error(`failed to reset because: ` + JSON.stringify(error));
});
```
## wallpaper.restore<sup>9+</sup>
restore(wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
移除指定类型的壁纸,恢复为默认显示的壁纸。
**需要权限**:ohos.permission.SET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,调用成功则返回是否移除成功的结果,调用失败则返回error信息。 |
**示例:**
```js
wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
if (error) {
console.error(`failed to restore because: ` + JSON.stringify(error));
return;
}
console.log(`success to restore.`);
});
```
## wallpaper.restore<sup>9+</sup>
restore(wallpaperType: WallpaperType): Promise&lt;void&gt;
移除指定类型的壁纸,恢复为默认显示的壁纸。
**需要权限**:ohos.permission.SET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 调用成功则返回是否移除成功的结果,调用失败则返回error信息。 |
**示例:**
```js
wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
console.log(`success to restore.`);
}).catch((error) => { }).catch((error) => {
console.error(`failed to restore because: ` + JSON.stringify(error)); console.error(`failed to isOperationAllowed because: ${JSON.stringify(error)}`);
}); });
``` ```
## wallpaper.setWallpaper ## wallpaper.reset<sup>(deprecated)</sup>
setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void reset(wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
将指定资源设置为指定类型的壁纸。 移除指定类型的壁纸,恢复为默认显示的壁纸。
> **说明:** > **说明:**
> >
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.setImage<sup>9+</sup>](#wallpapersetimage9)代替。 > 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.restore<sup>9+</sup>](#wallpaperrestore9)代替。
**需要权限**:ohos.permission.SET_WALLPAPER **需要权限**:ohos.permission.SET_WALLPAPER
...@@ -668,54 +866,30 @@ setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType, call ...@@ -668,54 +866,30 @@ setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType, call
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | | wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,调用成功则返回是返回设置的结果,调用失败则返回error信息。 | | callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,移除壁纸成功,error为undefined 否则返回error信息。 |
**示例:** **示例:**
```js
// source类型为string
let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
if (error) {
console.error(`failed to setWallpaper because: ` + JSON.stringify(error));
return;
}
console.log(`success to setWallpaper.`);
});
// source类型为image.PixelMap ```js
import image from '@ohos.multimedia.image'; wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
let imageSource = image.createImageSource("file://" + wallpaperPath); if (error) {
let opts = { console.error(`failed to reset because: ${JSON.stringify(error)}`);
"desiredSize": { return;
"height": 3648,
"width": 2736
} }
}; console.log(`success to reset.`);
imageSource.createPixelMap(opts).then((pixelMap) => {
wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
if (error) {
console.error(`failed to setWallpaper because: ` + JSON.stringify(error));
return;
}
console.log(`success to setWallpaper.`);
});
}).catch((error) => {
console.error(`failed to createPixelMap because: ` + JSON.stringify(error));
}); });
``` ```
## wallpaper.setWallpaper ## wallpaper.reset<sup>(deprecated)</sup>
setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise&lt;void&gt; reset(wallpaperType: WallpaperType): Promise&lt;void&gt;
将指定资源设置为指定类型的壁纸。 移除指定类型的壁纸,恢复为默认显示的壁纸。
> **说明:** > **说明:**
> >
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.setImage<sup>9+</sup>](#wallpapersetimage9)代替。 > 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.restore<sup>9+</sup>](#wallpaperrestore9)代替。
**需要权限**:ohos.permission.SET_WALLPAPER **需要权限**:ohos.permission.SET_WALLPAPER
...@@ -725,53 +899,34 @@ setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType): Pro ...@@ -725,53 +899,34 @@ setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType): Pro
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | | wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | 调用成功则返回是返回设置的结果,调用失败则返回error信息。 | | Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
**示例:** **示例:**
```js ```js
// source类型为string wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg"; console.log(`success to reset.`);
wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => { }).catch((error) => {
console.log(`success to setWallpaper.`); console.error(`failed to reset because: ${JSON.stringify(error)}`);
}).catch((error) => {
console.error(`failed to setWallpaper because: ` + JSON.stringify(error));
});
// source类型为image.PixelMap
import image from '@ohos.multimedia.image';
let imageSource = image.createImageSource("file://" + wallpaperPath);
let opts = {
"desiredSize": {
"height": 3648,
"width": 2736
}
};
imageSource.createPixelMap(opts).then((pixelMap) => {
wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
console.log(`success to setWallpaper.`);
}).catch((error) => {
console.error(`failed to setWallpaper because: ` + JSON.stringify(error));
});
}).catch((error) => {
console.error(`failed to createPixelMap because: ` + JSON.stringify(error));
}); });
``` ```
## wallpaper.setWallpaper<sup>(deprecated)</sup>
## wallpaper.setImage<sup>9+</sup> setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
setImage(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
将指定资源设置为指定类型的壁纸。 将指定资源设置为指定类型的壁纸。
> **说明:**
>
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.setImage<sup>9+</sup>](#wallpapersetimage9)代替。
**需要权限**:ohos.permission.SET_WALLPAPER **需要权限**:ohos.permission.SET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper **系统能力**: SystemCapability.MiscServices.Wallpaper
...@@ -782,21 +937,21 @@ setImage(source: string | image.PixelMap, wallpaperType: WallpaperType, callback ...@@ -782,21 +937,21 @@ setImage(source: string | image.PixelMap, wallpaperType: WallpaperType, callback
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 | | source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | | wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,调用成功则返回是返回设置的结果,调用失败则返回error信息。 | | callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,设置壁纸成功,error为undefined 否则返回error信息。 |
**示例:** **示例:**
```js ```js
// source类型为string // source类型为string
let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg"; let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => { wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
if (error) { if (error) {
console.error(`failed to setImage because: ` + JSON.stringify(error)); console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
return; return;
} }
console.log(`success to setImage.`); console.log(`success to setWallpaper.`);
}); });
// source类型为image.PixelMap // source类型为image.PixelMap
import image from '@ohos.multimedia.image'; import image from '@ohos.multimedia.image';
let imageSource = image.createImageSource("file://" + wallpaperPath); let imageSource = image.createImageSource("file://" + wallpaperPath);
...@@ -806,25 +961,29 @@ let opts = { ...@@ -806,25 +961,29 @@ let opts = {
"width": 2736 "width": 2736
} }
}; };
imageSource.createPixelMap(opts).then((pixelMap) => { imageSource.createPixelMap(opts).then((pixelMap) => {
wallpaper.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => { wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
if (error) { if (error) {
console.error(`failed to setImage because: ` + JSON.stringify(error)); console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
return; return;
} }
console.log(`success to setImage.`); console.log(`success to setWallpaper.`);
}); });
}).catch((error) => { }).catch((error) => {
console.error(`failed to createPixelMap because: ` + JSON.stringify(error)); console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
}); });
``` ```
## wallpaper.setImage<sup>9+</sup> ## wallpaper.setWallpaper<sup>(deprecated)</sup>
setImage(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise&lt;void&gt; setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise&lt;void&gt;
将指定资源设置为指定类型的壁纸。 将指定资源设置为指定类型的壁纸。
> **说明:**
>
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.setImage<sup>9+</sup>](#wallpapersetimage9)代替。
**需要权限**:ohos.permission.SET_WALLPAPER **需要权限**:ohos.permission.SET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper **系统能力**: SystemCapability.MiscServices.Wallpaper
...@@ -840,17 +999,17 @@ setImage(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise ...@@ -840,17 +999,17 @@ setImage(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise
| 类型 | 说明 | | 类型 | 说明 |
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | 调用成功则返回是返回设置的结果,调用失败则返回error信息。 | | Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
**示例:** **示例:**
```js ```js
// source类型为string // source类型为string
let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg"; let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => { wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
console.log(`success to setImage.`); console.log(`success to setWallpaper.`);
}).catch((error) => { }).catch((error) => {
console.error(`failed to setImage because: ` + JSON.stringify(error)); console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
}); });
// source类型为image.PixelMap // source类型为image.PixelMap
...@@ -862,18 +1021,19 @@ let opts = { ...@@ -862,18 +1021,19 @@ let opts = {
"width": 2736 "width": 2736
} }
}; };
imageSource.createPixelMap(opts).then((pixelMap) => { imageSource.createPixelMap(opts).then((pixelMap) => {
wallpaper.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => { wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
console.log(`success to setImage.`); console.log(`success to setWallpaper.`);
}).catch((error) => { }).catch((error) => {
console.error(`failed to setImage because: ` + JSON.stringify(error)); console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
}); });
}).catch((error) => { }).catch((error) => {
console.error(`failed to createPixelMap because: ` + JSON.stringify(error)); console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
}); });
``` ```
## wallpaper.getFile<sup>8+</sup>
## wallpaper.getFile<sup>(deprecated)</sup>
getFile(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void getFile(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void
...@@ -895,18 +1055,18 @@ getFile(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): vo ...@@ -895,18 +1055,18 @@ getFile(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): vo
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 | | callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 |
**示例:** **示例:**
```js ```js
wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => { wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
if (error) { if (error) {
console.error(`failed to getFile because: ` + JSON.stringify(error)); console.error(`failed to getFile because: ${JSON.stringify(error)}`);
return; return;
} }
console.log(`success to getFile: ` + JSON.stringify(data)); console.log(`success to getFile: ${JSON.stringify(data)}`);
}); });
``` ```
## wallpaper.getFile<sup>8+</sup> ## wallpaper.getFile<sup>(deprecated)</sup>
getFile(wallpaperType: WallpaperType): Promise&lt;number&gt; getFile(wallpaperType: WallpaperType): Promise&lt;number&gt;
...@@ -933,44 +1093,16 @@ getFile(wallpaperType: WallpaperType): Promise&lt;number&gt; ...@@ -933,44 +1093,16 @@ getFile(wallpaperType: WallpaperType): Promise&lt;number&gt;
| Promise&lt;number&gt; | 调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 | | Promise&lt;number&gt; | 调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 |
**示例:** **示例:**
```js ```js
wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => { wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
console.log(`success to getFile: ` + JSON.stringify(data)); console.log(`success to getFile: ${JSON.stringify(data)}`);
}).catch((error) => { }).catch((error) => {
console.error(`failed to getFile because: ` + JSON.stringify(error)); console.error(`failed to getFile because: ${JSON.stringify(error)}`);
}); });
``` ```
## wallpaper.getFileSync<sup>9+</sup> ## wallpaper.getPixelMap<sup>(deprecated)</sup>
getFileSync(wallpaperType: WallpaperType): number;
获取指定类型的壁纸文件。
**需要权限**:ohos.permission.GET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| number | 调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 |
**示例:**
```js
let file = wallpaper.getFileSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM);
```
## wallpaper.getPixelMap
getPixelMap(wallpaperType: WallpaperType, callback: AsyncCallback&lt;image.PixelMap&gt;): void; getPixelMap(wallpaperType: WallpaperType, callback: AsyncCallback&lt;image.PixelMap&gt;): void;
...@@ -994,15 +1126,18 @@ getPixelMap(wallpaperType: WallpaperType, callback: AsyncCallback&lt;image.Pixel ...@@ -994,15 +1126,18 @@ getPixelMap(wallpaperType: WallpaperType, callback: AsyncCallback&lt;image.Pixel
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是 | 回调函数,调用成功则返回壁纸图片的像素图大小,调用失败则返回error信息。 | | callback | AsyncCallback&lt;image.PixelMap&gt; | 是 | 回调函数,调用成功则返回壁纸图片的像素图大小,调用失败则返回error信息。 |
**示例:** **示例:**
```js ```js
wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM, function (err, data) { wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM, function (error, data) {
console.info('wallpaperXTS ===> testGetPixelMapCallbackSystem err : ' + JSON.stringify(err)); if (error) {
console.info('wallpaperXTS ===> testGetPixelMapCallbackSystem data : ' + JSON.stringify(data)); console.error(`failed to getPixelMap because: ${JSON.stringify(error)}`);
return;
}
console.log(`success to getPixelMap : ${JSON.stringify(data)}`);
}); });
``` ```
## wallpaper.getPixelMap ## wallpaper.getPixelMap<sup>(deprecated)</sup>
getPixelMap(wallpaperType: WallpaperType): Promise&lt;image.PixelMap&gt; getPixelMap(wallpaperType: WallpaperType): Promise&lt;image.PixelMap&gt;
...@@ -1031,141 +1166,11 @@ getPixelMap(wallpaperType: WallpaperType): Promise&lt;image.PixelMap&gt; ...@@ -1031,141 +1166,11 @@ getPixelMap(wallpaperType: WallpaperType): Promise&lt;image.PixelMap&gt;
| Promise&lt;image.PixelMap&gt; | 调用成功则返回壁纸图片的像素图大小,调用失败则返回error信息。 | | Promise&lt;image.PixelMap&gt; | 调用成功则返回壁纸图片的像素图大小,调用失败则返回error信息。 |
**示例:** **示例:**
```js
wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
console.info('wallpaperXTS ===> testGetPixelMapPromiseSystem data : ' + data);
console.info('wallpaperXTS ===> testGetPixelMapPromiseSystem data : ' + JSON.stringify(data));
}).catch((err) => {
console.info('wallpaperXTS ===> testGetPixelMapPromiseSystem err : ' + err);
console.info('wallpaperXTS ===> testGetPixelMapPromiseSystem err : ' + JSON.stringify(err));
});
```
## wallpaper.getImage<sup>9+</sup>
getImage(wallpaperType: WallpaperType, callback: AsyncCallback&lt;image.PixelMap&gt;): void;
获取壁纸图片的像素图。
**需要权限**:ohos.permission.GET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**系统接口**:此接口为系统接口。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | 是 | 回调函数,调用成功则返回壁纸图片的像素图大小,调用失败则返回error信息。 |
**示例:**
```js
wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM, function (err, data) {
console.info('wallpaperXTS ===> testgetImageCallbackSystem err : ' + JSON.stringify(err));
console.info('wallpaperXTS ===> testgetImageCallbackSystem data : ' + JSON.stringify(data));
});
```
## wallpaper.getImage<sup>9+</sup>
getImage(wallpaperType: WallpaperType): Promise&lt;image.PixelMap&gt;
获取壁纸图片的像素图。
**需要权限**:ohos.permission.GET_WALLPAPER
**系统能力**: SystemCapability.MiscServices.Wallpaper
**系统接口**:此接口为系统接口。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | 调用成功则返回壁纸图片的像素图大小,调用失败则返回error信息。 |
**示例:**
```js ```js
wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => { wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
console.info('wallpaperXTS ===> testgetImagePromiseSystem data : ' + data); console.log(`success to getPixelMap : ${JSON.stringify(data)}`);
console.info('wallpaperXTS ===> testgetImagePromiseSystem data : ' + JSON.stringify(data)); }).catch((error) => {
}).catch((err) => { console.error(`failed to getPixelMap because: ${JSON.stringify(error)}`);
console.info('wallpaperXTS ===> testgetImagePromiseSystem err : ' + err);
console.info('wallpaperXTS ===> testgetImagePromiseSystem err : ' + JSON.stringify(err));
}); });
``` ```
## wallpaper.on('colorChange')<sup>9+</sup>
on(type: 'colorChange', callback: (colors: Array&lt;RgbaColor&gt;, wallpaperType: WallpaperType) =&gt; void): void
订阅壁纸颜色变化结果上报事件。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| type | string | 是 | 取值为'colorChange',表示壁纸颜色变化结果上报事件。 |
| callback | function | 是 | 壁纸颜色变化触发该回调方法,返回壁纸类型和壁纸的主要颜色信息。<br/>- colors<br/> 壁纸的主要颜色信息,其类型见[RgbaColor](#rgbacolor)<br/>- wallpaperType<br/> 壁纸类型。 |
**示例:**
```js
let listener = (colors, wallpaperType) => {
console.log(`wallpaper color changed.`);
};
wallpaper.on('colorChange', listener);
```
## wallpaper.off('colorChange')<sup>9+</sup>
off(type: 'colorChange', callback?: (colors: Array&lt;RgbaColor&gt;, wallpaperType: WallpaperType) =&gt; void): void
取消订阅壁纸颜色变化结果上报事件。
**系统能力**: SystemCapability.MiscServices.Wallpaper
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| type | string | 是 | 取值为'colorChange',表示取消订阅壁纸颜色变化结果上报事件。 |
| callback | function | 否 | 表示取消壁纸颜色变化结果上报,不填写该参数则取消订阅该type对应的所有回调。<br/>- colors<br/> 壁纸的主要颜色信息,其类型见[RgbaColor](#rgbacolor)<br/>- wallpaperType<br/> 壁纸类型。 |
**示例:**
```js
let listener = (colors, wallpaperType) => {
console.log(`wallpaper color changed.`);
};
wallpaper.on('colorChange', listener);
// 取消订阅listener
wallpaper.off('colorChange', listener);
// 取消所有'colorChange'类型的订阅
wallpaper.off('colorChange');
```
## RgbaColor
**系统能力**: SystemCapability.MiscServices.Wallpaper
| 名称 | 类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
| red | number | 是 | 是 | 表示红色值,范围为 0 到 255。 |
| green | number | 是 | 是 | 表示绿色值,范围为 0 到 255。 |
| blue | number | 是 | 是 | 表示蓝色值,范围为 0 到 255。 |
| alpha | number | 是 | 是 | 表示 alpha 值,范围为 0 到 255。 |
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册