From 4383fa3659d5815db08de16d2a33c5c1c1be135d Mon Sep 17 00:00:00 2001 From: zgf Date: Thu, 10 Oct 2024 10:11:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Epreload=E9=A2=84=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=92=8Ccancel=E5=8F=96=E6=B6=88=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=B7=BB=E5=8A=A0prefetch=E9=85=8D?= =?UTF-8?q?=E5=90=88preload=E6=8E=A5=E5=8F=A3=E6=A0=B7=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zgf --- CHANGELOG.md | 5 + README_zh.md | 6 +- entry/src/main/ets/pages/Index.ets | 5 + entry/src/main/ets/pages/LazyForEachCache.ets | 51 ++++++ entry/src/main/ets/pages/LazyForEachCount.ets | 51 ++++++ .../main/ets/pages/PrefetchAndCacheCount.ets | 39 +++++ .../src/main/ets/pages/PrefetchAndPreload.ets | 56 +++++++ entry/src/main/ets/pages/UserPage.ets | 41 +---- .../main/ets/pages/model/CommonDataSource.ets | 54 +++++++ .../ets/pages/model/DataSourcePrefetching.ets | 93 +++++++++++ .../main/ets/pages/model/PageViewModel.ets | 146 ++++++++++++++++++ .../resources/base/profile/main_pages.json | 6 +- library/oh-package.json5 | 2 +- library/src/main/ets/ImageKnife.ets | 35 ++++- 14 files changed, 544 insertions(+), 46 deletions(-) create mode 100644 entry/src/main/ets/pages/LazyForEachCache.ets create mode 100644 entry/src/main/ets/pages/LazyForEachCount.ets create mode 100644 entry/src/main/ets/pages/PrefetchAndCacheCount.ets create mode 100644 entry/src/main/ets/pages/PrefetchAndPreload.ets create mode 100644 entry/src/main/ets/pages/model/CommonDataSource.ets create mode 100644 entry/src/main/ets/pages/model/DataSourcePrefetching.ets create mode 100644 entry/src/main/ets/pages/model/PageViewModel.ets diff --git a/CHANGELOG.md b/CHANGELOG.md index 83fdfde..40a1d80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 3.0.2 +- 新增图片重新加载接口reload +- 新增返回请求request预加载接口preload +- 新增取消请求接口cancel + ## 3.0.2-rc.2 - ImageKnifeAnimatorComponent新增开始、结束、暂停的回调事件 - 文件缓存数量负数和超过INT最大值时默认为INT最大值 diff --git a/README_zh.md b/README_zh.md index 698a31a..c716928 100644 --- a/README_zh.md +++ b/README_zh.md @@ -315,8 +315,10 @@ ImageKnifeAnimatorComponent({ |-------------------|-------------------------------------------------------------------------------------------------------|------------------| | initMemoryCache | newMemoryCache: IMemoryCache | 自定义内存缓存策略 | | initFileCache | context: Context, size: number, memory: number | 初始化文件缓存数量和大小 | -| reload | request: ImageKnifeRequest | 图片重新加载 | -| preLoadCache | loadSrc: string I ImageKnifeOption | 预加载并返回文件缓存路径 | +| reload | request: ImageKnifeRequest | 图片重新加载 | +| preLoad | loadSrc: string I ImageKnifeOption | 预加载返回图片请求request | +| cancel | request: ImageKnifeRequest | 取消图片请求 | +| preLoadCache | loadSrc: string I ImageKnifeOption | 预加载并返回文件缓存路径 | | getCacheImage | loadSrc: string, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string) | 从内存或文件缓存中获取资源 | | addHeader | key: string, value: Object | 全局添加http请求头 | | setHeaderOptions | Array | 全局设置http请求头 | diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 209d789..96d6a41 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -40,6 +40,11 @@ struct Index { uri: 'pages/ImageKnifeReload', }); }) + Button("动态预加载prefetch").margin({top:10}).onClick(()=>{ + router.push({ + uri: 'pages/PrefetchAndCacheCount', + }); + }) Button($r('app.string.Test_multiple_images')).margin({top:10}).onClick(()=>{ router.push({ uri: 'pages/TestCommonImage', diff --git a/entry/src/main/ets/pages/LazyForEachCache.ets b/entry/src/main/ets/pages/LazyForEachCache.ets new file mode 100644 index 0000000..29bdea2 --- /dev/null +++ b/entry/src/main/ets/pages/LazyForEachCache.ets @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2024 Huawei 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. + */ +import { InfoItem } from './model/DataSourcePrefetching'; +import { PageViewModel } from './model/PageViewModel'; +import { ImageKnifeComponent, ImageKnifeOption } from '@ohos/libraryimageknife'; +import { CommonDataSource } from './model/CommonDataSource'; + +@Entry +@Component +export struct LazyForEachCache { + @State hotCommendList:CommonDataSource = new CommonDataSource([]) + aboutToAppear(): void { + this.hotCommendList.addData(this.hotCommendList.totalCount(),PageViewModel.getItems()) + } + build() { + Column() { + List({ space: 16 }) { + LazyForEach(this.hotCommendList, (item: InfoItem,index) => { + ListItem() { + Column({ space: 12 }) { + ImageKnifeComponent({ + imageKnifeOption:{ + loadSrc: item.albumUrl, + placeholderSrc:$r('app.media.loading') + } + }).width(100).height(100) + Text(`${index}`) + }.border({ width: 5 , color: "#000000"}) + } + }) + } + .cachedCount(5) + .width("100%") + .height("100%") + .margin({ left: 10, right: 10 }) + .layoutWeight(1) + } + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/LazyForEachCount.ets b/entry/src/main/ets/pages/LazyForEachCount.ets new file mode 100644 index 0000000..31995eb --- /dev/null +++ b/entry/src/main/ets/pages/LazyForEachCount.ets @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2024 Huawei 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. + */ +import { InfoItem } from './model/DataSourcePrefetching'; +import { PageViewModel } from './model/PageViewModel'; +import { ImageKnifeComponent, ImageKnifeOption } from '@ohos/libraryimageknife'; +import { CommonDataSource } from './model/CommonDataSource'; + +@Entry +@Component +export struct LazyForEachCount { + @State hotCommendList:CommonDataSource = new CommonDataSource([]) + aboutToAppear(): void { + this.hotCommendList.addData(this.hotCommendList.totalCount(),PageViewModel.getItems()) + } + build() { + Column() { + List({ space: 16 }) { + LazyForEach(this.hotCommendList, (item: InfoItem,index) => { + ListItem() { + Column({ space: 12 }) { + ImageKnifeComponent({ + imageKnifeOption:{ + loadSrc: item.albumUrl, + placeholderSrc:$r('app.media.loading') + } + }).width(100).height(100) + Text(`${index}`) + }.border({ width: 5 , color: "#000000"}) + } + }) + } + .cachedCount(30) + .width("100%") + .height("100%") + .margin({ left: 10, right: 10 }) + .layoutWeight(1) + } + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/PrefetchAndCacheCount.ets b/entry/src/main/ets/pages/PrefetchAndCacheCount.ets new file mode 100644 index 0000000..00c3f08 --- /dev/null +++ b/entry/src/main/ets/pages/PrefetchAndCacheCount.ets @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2024 Huawei 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. + */ +import { router } from '@kit.ArkUI' + +@Entry +@Component +struct PrefetchAndCacheCount { + build() { + Column() { + Button("cacheCount == 5") + .onClick(()=>{ + router.pushUrl({url:"pages/LazyForEachCache"}) + }) + Button("cacheCount == 30") + .margin({top:10}) + .onClick(()=>{ + router.pushUrl({url:"pages/LazyForEachCount"}) + }) + Button("prefetch + preload") + .margin({top:10}) + .onClick(()=>{ + router.pushUrl({url:"pages/PrefetchAndPreload"}) + }) + }.width("100%") + .height("100%") + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/PrefetchAndPreload.ets b/entry/src/main/ets/pages/PrefetchAndPreload.ets new file mode 100644 index 0000000..390380a --- /dev/null +++ b/entry/src/main/ets/pages/PrefetchAndPreload.ets @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2024 Huawei 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. + */ +import { BasicPrefetcher } from '@kit.ArkUI'; +import DataSourcePrefetchingImageKnife, { InfoItem } from './model/DataSourcePrefetching'; +import { PageViewModel } from './model/PageViewModel'; +import { ImageKnifeComponent, ImageKnifeOption } from '@ohos/libraryimageknife'; +@Entry +@Component +export struct PrefetchAndPreload { + // 创建DataSourcePrefetchingImageKnife对象,具备任务预取、取消能力的数据源 + private readonly dataSource = new DataSourcePrefetchingImageKnife(PageViewModel.getItems()); + // 创建BasicPrefetcher对象,默认的动态预取算法实现 + private readonly prefetcher = new BasicPrefetcher(this.dataSource); + + build() { + Column() { + List({ space: 16 }) { + LazyForEach(this.dataSource, (item: InfoItem,index:number) => { + ListItem() { + Column({ space: 12 }) { + ImageKnifeComponent({ + imageKnifeOption:{ + loadSrc: item.albumUrl, + placeholderSrc:$r('app.media.loading') + } + }).width(100).height(100) + Text(`${index}`) + }.border({ width: 5 , color: "#000000"}) + } + .reuseId('imageKnife') + }) + } + .cachedCount(5) + .onScrollIndex((start: number, end: number) => { + // 列表滚动触发visibleAreaChanged,实时更新预取范围,触发调用prefetch、cancel接口 + this.prefetcher.visibleAreaChanged(start, end) + }) + .width("100%") + .height("100%") + .margin({ left: 10, right: 10 }) + .layoutWeight(1) + } + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/UserPage.ets b/entry/src/main/ets/pages/UserPage.ets index ee210bc..80958a5 100644 --- a/entry/src/main/ets/pages/UserPage.ets +++ b/entry/src/main/ets/pages/UserPage.ets @@ -12,48 +12,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { CommonDataSource } from './model/CommonDataSource' import { UserAvatar } from './User' -class CommonDataSource implements IDataSource { - private dataArray: T[] = [] - private listeners: DataChangeListener[] = [] - - constructor(element: []) { - this.dataArray = element - } - - public getData(index: number) { - return this.dataArray[index] - } - - public totalCount(): number { - return this.dataArray.length - } - - public addData(index: number, data: T[]): void { - this.dataArray = this.dataArray.concat(data) - this.notifyDataAdd(index) - } - - unregisterDataChangeListener(listener: DataChangeListener): void { - const pos = this.listeners.indexOf(listener); - if (pos >= 0) { - this.listeners.splice(pos, 1); - } - } - - registerDataChangeListener(listener: DataChangeListener): void { - if (this.listeners.indexOf(listener) < 0) { - this.listeners.push(listener) - } - } - - notifyDataAdd(index: number): void { - this.listeners.forEach((listener: DataChangeListener) => { - listener.onDataAdd(index) - }) - } -} @Entry @Component struct Index { diff --git a/entry/src/main/ets/pages/model/CommonDataSource.ets b/entry/src/main/ets/pages/model/CommonDataSource.ets new file mode 100644 index 0000000..d6b16c0 --- /dev/null +++ b/entry/src/main/ets/pages/model/CommonDataSource.ets @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2024 Huawei 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. + */ +export class CommonDataSource implements IDataSource { + private dataArray: T[] = [] + private listeners: DataChangeListener[] = [] + + constructor(element: []) { + this.dataArray = element + } + + public getData(index: number) { + return this.dataArray[index] + } + + public totalCount(): number { + return this.dataArray.length + } + + public addData(index: number, data: T[]): void { + this.dataArray = this.dataArray.concat(data) + this.notifyDataAdd(index) + } + + unregisterDataChangeListener(listener: DataChangeListener): void { + const pos = this.listeners.indexOf(listener); + if (pos >= 0) { + this.listeners.splice(pos, 1); + } + } + + registerDataChangeListener(listener: DataChangeListener): void { + if (this.listeners.indexOf(listener) < 0) { + this.listeners.push(listener) + } + } + + notifyDataAdd(index: number): void { + this.listeners.forEach((listener: DataChangeListener) => { + listener.onDataAdd(index) + }) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/model/DataSourcePrefetching.ets b/entry/src/main/ets/pages/model/DataSourcePrefetching.ets new file mode 100644 index 0000000..07e0101 --- /dev/null +++ b/entry/src/main/ets/pages/model/DataSourcePrefetching.ets @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2024 Huawei 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. + */ +import {ImageKnife, ImageKnifeRequest} from "@ohos/libraryimageknife" +import { IDataSourcePrefetching } from '@kit.ArkUI'; +import { HashMap } from '@kit.ArkTS'; + +const IMADE_UNAVAILABLE = $r('app.media.failed') +export interface InfoItem { + albumUrl: string | Resource +} +export default class DataSourcePrefetchingImageKnife implements IDataSourcePrefetching { + private dataArray: Array + private readonly requestList: HashMap = new HashMap() + private listeners: DataChangeListener[] = []; + + constructor(dataArray: Array) { + this.dataArray = dataArray; + } + public getData(index: number) { + return this.dataArray[index] + } + + public totalCount(): number { + return this.dataArray.length + } + + public addData(index: number, data: InfoItem[]): void { + this.dataArray = this.dataArray.concat(data) + this.notifyDataAdd(index) + } + + unregisterDataChangeListener(listener: DataChangeListener): void { + const pos = this.listeners.indexOf(listener); + if (pos >= 0) { + this.listeners.splice(pos, 1); + } + } + + registerDataChangeListener(listener: DataChangeListener): void { + if (this.listeners.indexOf(listener) < 0) { + this.listeners.push(listener) + } + } + + notifyDataAdd(index: number): void { + this.listeners.forEach((listener: DataChangeListener) => { + listener.onDataAdd(index) + }) + } + async prefetch(index: number): Promise { + let item = this.dataArray[index] + if (typeof item.albumUrl == "string") { + // 图片预加载 + let request = ImageKnife.getInstance().preload({ + loadSrc:item.albumUrl, + onLoadListener:{ + onLoadSuccess:()=>{ + // 预加载成功,删除成功请求 + this.requestList.remove(index) + }, + onLoadFailed:()=>{ + // 移除失败请求 + this.requestList.remove(index) + } + } + }) + this.requestList.set(index,request) + } + } + // 取消请求处理 + cancel(index: number) { + if(this.requestList.hasKey(index)) { + // 返回MAP对象指定元素 + const request = this.requestList.get(index) + // 取消请求 + ImageKnife.getInstance().cancel(request) + // 移除被取消的请求对象 + this.requestList.remove(index) + } + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/model/PageViewModel.ets b/entry/src/main/ets/pages/model/PageViewModel.ets new file mode 100644 index 0000000..7a33e3a --- /dev/null +++ b/entry/src/main/ets/pages/model/PageViewModel.ets @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2024 Huawei 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. + */ +import { InfoItem } from './DataSourcePrefetching' + +export class PageViewModel { + private static dataArray: Array = [ + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/56/v3/8MdhfSsCSMKj4sA6okUWrg/5uBx56tLTUO3RYQl-E5JiQ.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/e2/v3/4zI1Xm_3STmV30aZXWRrKw/6aN7WodDRUiBApgffiLPCg.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/42/v3/2dSQCqERTP2TTPyssOMEbQ/zL1ebnKKQ_ilqTDcwCAkOw.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/78/v3/qQJpAtRGQe2e_VhbGHDgIw/b3zlit99S6GybD3XdNwqJw.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/55/v3/5DZ2LLqYSsK85-shqgLveQ/7ZXcyCWNTvOzQP5FFLBGkg.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/3e/v3/LqRoLI-PRSu9Nqa8KdJ-pQ/dSqskBpSR9eraAMn7NBdqA.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/25/v3/jgB2ekkTRX-3yTYZalnANQ/xff_x9cbSPqb7fbNwgJa7A.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/fb/v3/alXwXLHKSyCAIWt_ydgD2g/BCCuu25TREOitQxM7eYOEw.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/63/v3/qbe6NZkCQyGcITvdWoZBgg/Y-5U1z3GT_yaK8CBD3jkwg.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/16/v3/fm2tO4TsRH6mv_D_nSSd5w/FscLpLwQQ-KuV7oaprFK2Q.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/89/v3/UAUvtPHqRD-GWWANsEC57Q/zcRJCQebQ322Aby4jzmwmQ.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/30/v3/tUUzzx73R4yp8G--lMhuWQ/EBbcu_dLTT-Jj68XAh6mtA.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/76/v3/EyF6z4FISpCHhae38eEexw/OtyAiu-zSSevNQYvUdtVmA.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/37/v3/12rH1yiEQmK9wlOOcy5avQ/RzBXiEBRRqOC7LRkwNj6VA.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/9a/v3/TpRN4AIzRoyUXIqWdKoE0g/ShOnD_tfS46HDbpSWhbCkQ.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/03/v3/H3X17s8eTdS2w56JgbB5jQ/a45sT-j8Sbe8sSQXTzeYvQ.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/10/v3/qaEzwkU0QeKb1yehnP2Xig/q7fxAlgMQKup-HUBayRLGQ.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/96/v3/rMJJoAflTDSWa1z2pHs2wg/8dOqD0GlQBOCL5AvQok9FQ.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/ed/v3/KMO4D6D2QGuVOCLX4AhOFA/ef51xAaLQuK7BsnuD9abog.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/d9/v3/FSZH0aTdSqWxeAaxoPvi0g/RqxPxUCXQFiTMBfKTF9kkw.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/bf/v3/lSjrRwFcS-ez6jp1ALSQFg/0n7R7XinSPyrYLqDu_1dfw.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/8a/v3/ZKzYV5BJTuCk5hCE0y_xNA/8JT95OQnSZSd6_xQQUONhQ.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/1/v3/sTXb_I7URBKjdMyLDYa19w/qpcwa_FNQmi3-EzjbGsJ8A.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/e5/v3/m7wFvw_eQIuDV0Mk0IKi8g/gJU4migzTHKYk5KrgdZbBw.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/3f/v3/k_UWbB5_RGW7JemQZ0OQdw/_DUdmaZRQyG-Oyvkb663Bw.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/39/v3/rFRN7G_VSo-p4mBjTZtkRw/gBwTI-ieSIqSsSmLNBEcgw.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/04/v3/6K8BPYKVQFOr7KCuAG9nog/qKd3pZlrQy2M-feB3ycVPA.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/7d/v3/f0GQFzm1T6eduVeMUhO3Wg/-4cvzIJiRCegjIno3ofIbQ.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/e4/v3/C0xxsSeySxW-2iYR5OEbpQ/f1GlaD3zTeKPX8Vd-M1oVQ.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/c2/v3/32LCyXN4TuWKWcdf9gAwWw/ej14_BCJQNCaWOKoI9aZAw.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/fd/v3/LyYJMdMmQNaC5GyBYEZ5Pw/uFLiovypRSagKyIS-UJPVw.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/15/v3/MHM9KaWGTgubn6M8-B_6nw/1YO9JyYhTHSBWsoiqYkGZw.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/4c/v3/UdYfbv1_QYqn_ulDHp89OA/VkjexMluTqGO3yt3gPK1DA.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/e8/v3/N8blT_7qSK-tRtahIyov7g/M_kjGEEmSzOlTc47Zrfozg.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/28/v3/VS_h3m4YRrSgbgxnqE3vtQ/h-2Q1Qy2SSGEuXM36-Rq_w.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/2e/v3/R-BaM5ToRNGq5rwtNTcnww/Q2e01VHiR2y9KtFaZmpmNQ.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/88/v3/3djkAJKKTdC539XqMdstSg/wHO7DxvXQS2xbt2Y_-4BNg.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/fb/v3/guw4eiggR3uWjscFTxITYg/TzRB35iPTdCztrZUUaNuFg.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/93/v3/UvSh_f1LT66i0-3hvsYN_A/eYnE3Z8YT5Sk7F-vS2ZmCQ.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/5/v3/tv8Vqf9hQrKpozGeZWg2mw/VEICB-bmQYi0Iv6TGADbhw.jpg" + }, + { + albumUrl: "https://contentcenter-drcn.dbankcdn.cn/pub_1/DevEcoSpace_1_900_9/30/v3/4v1Ot5BRR6OFVQ9MGn9Xxg/xrPgRn0LS1ep-r7ewIuwiw.jpg" + }, + ] + static getItems() { + return PageViewModel.dataArray + } +} \ No newline at end of file diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index c8098d3..da1a3f5 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -24,6 +24,10 @@ "pages/TestSetCustomImagePage", "pages/TestErrorHolderPage", "pages/TestTaskResourcePage", - "pages/ImageKnifeReload" + "pages/ImageKnifeReload", + "pages/LazyForEachCount", + "pages/LazyForEachCache", + "pages/PrefetchAndCacheCount", + "pages/PrefetchAndPreload" ] } \ No newline at end of file diff --git a/library/oh-package.json5 b/library/oh-package.json5 index 9817c90..9fdcdc5 100644 --- a/library/oh-package.json5 +++ b/library/oh-package.json5 @@ -14,7 +14,7 @@ "main": "index.ets", "repository": "https://gitee.com/openharmony-tpc/ImageKnife", "type": "module", - "version": "3.0.2-rc.2", + "version": "3.0.2", "dependencies": { "@ohos/gpu_transform": "^1.0.2" }, diff --git a/library/src/main/ets/ImageKnife.ets b/library/src/main/ets/ImageKnife.ets index b36fad1..3e6cced 100644 --- a/library/src/main/ets/ImageKnife.ets +++ b/library/src/main/ets/ImageKnife.ets @@ -145,8 +145,39 @@ export class ImageKnife { let key = this.getEngineKeyImpl().generateMemoryKey(imageKnifeOption.loadSrc, ImageKnifeRequestSource.SRC, imageKnifeOption); this.memoryCache.remove(key); } - - + /** + * 预加载 + * @param loadSrc 图片地址url + * @returns 图片请求request + */ + preload(loadSrc:string | ImageKnifeOption):ImageKnifeRequest{ + let imageKnifeOption = new ImageKnifeOption() + if (typeof loadSrc == "string") { + imageKnifeOption.loadSrc = loadSrc + } else { + imageKnifeOption = loadSrc; + } + let request = new ImageKnifeRequest( + imageKnifeOption, + imageKnifeOption.context !== undefined ? imageKnifeOption.context : getContext(this) as common.UIAbilityContext, + 0, + 0, + 0, + { + showPixelMap(version: number, pixelMap: PixelMap | string) { + } + } + ) + this.execute(request) + return request + } + /** + * 取消图片请求 + * @param request 图片请求request + */ + cancel(request:ImageKnifeRequest) { + request.requestState = ImageKnifeRequestState.DESTROY + } /** * 预加载图片到文件缓存 * @param loadSrc 图片地址url