新增reload加载接口和网络恢复重新加载样例
Signed-off-by: zgf <zenggaofeng2@h-partners.com>
This commit is contained in:
parent
1787fffea7
commit
aa2cd9c1bf
|
@ -312,9 +312,10 @@ ImageKnifeAnimatorComponent({
|
||||||
### ImageKnife接口
|
### ImageKnife接口
|
||||||
|
|
||||||
| 参数名称 | 入参内容 | 功能简介 |
|
| 参数名称 | 入参内容 | 功能简介 |
|
||||||
|------------------|-------------------------------------------------------------------------------------------------------|---------------|
|
|-------------------|-------------------------------------------------------------------------------------------------------|------------------|
|
||||||
| initMemoryCache | newMemoryCache: IMemoryCache | 自定义内存缓存策略 |
|
| initMemoryCache | newMemoryCache: IMemoryCache | 自定义内存缓存策略 |
|
||||||
| initFileCache | context: Context, size: number, memory: number | 初始化文件缓存数量和大小 |
|
| initFileCache | context: Context, size: number, memory: number | 初始化文件缓存数量和大小 |
|
||||||
|
| reload | request: ImageKnifeRequest | 图片重新加载 |
|
||||||
| preLoadCache | loadSrc: string I ImageKnifeOption | 预加载并返回文件缓存路径 |
|
| preLoadCache | loadSrc: string I ImageKnifeOption | 预加载并返回文件缓存路径 |
|
||||||
| getCacheImage | loadSrc: string, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string) | 从内存或文件缓存中获取资源 |
|
| getCacheImage | loadSrc: string, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string) | 从内存或文件缓存中获取资源 |
|
||||||
| addHeader | key: string, value: Object | 全局添加http请求头 |
|
| addHeader | key: string, value: Object | 全局添加http请求头 |
|
||||||
|
@ -323,7 +324,7 @@ ImageKnifeAnimatorComponent({
|
||||||
| setCustomGetImage | customGetImage?: (context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined> | 全局设置自定义下载 |
|
| setCustomGetImage | customGetImage?: (context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined> | 全局设置自定义下载 |
|
||||||
| setEngineKeyImpl | IEngineKey | 全局配置缓存key生成策略 |
|
| setEngineKeyImpl | IEngineKey | 全局配置缓存key生成策略 |
|
||||||
| putCacheImage | url: string, pixelMap: PixelMap, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string | 写入内存磁盘缓存 |
|
| putCacheImage | url: string, pixelMap: PixelMap, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string | 写入内存磁盘缓存 |
|
||||||
| removeMemoryCache| url: string | ImageKnifeOption | 清理指定内存缓存 |
|
| removeMemoryCache | url: string | ImageKnifeOption | 清理指定内存缓存 |
|
||||||
| removeFileCache | url: string | ImageKnifeOption | 清理指定磁盘缓存 |
|
| removeFileCache | url: string | ImageKnifeOption | 清理指定磁盘缓存 |
|
||||||
### 图形变换类型(需要为GPUImage添加依赖项)
|
### 图形变换类型(需要为GPUImage添加依赖项)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
/*
|
||||||
|
* 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 { connection } from '@kit.NetworkKit'
|
||||||
|
import { List } from '@kit.ArkTS'
|
||||||
|
import { ImageKnifeRequest,ImageKnife,ImageKnifeComponent } from '@ohos/libraryimageknife'
|
||||||
|
|
||||||
|
@Entry
|
||||||
|
@Component
|
||||||
|
struct ImageKnifeReload {
|
||||||
|
aboutToAppear(): void {
|
||||||
|
NetWatchState.init()
|
||||||
|
}
|
||||||
|
build() {
|
||||||
|
Column() {
|
||||||
|
ImageKnifeComponent({
|
||||||
|
imageKnifeOption:{
|
||||||
|
loadSrc:"https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp",
|
||||||
|
placeholderSrc:$r('app.media.loading'),
|
||||||
|
errorholderSrc:$r('app.media.failed'),
|
||||||
|
onLoadListener:{
|
||||||
|
onLoadFailed(err,request){
|
||||||
|
NetWatchState.requestList.add(request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).width(200).height(200)
|
||||||
|
ImageKnifeComponent({
|
||||||
|
imageKnifeOption:{
|
||||||
|
loadSrc:"https://img-blog.csdn.net/20140514114029140",
|
||||||
|
placeholderSrc:$r('app.media.loading'),
|
||||||
|
errorholderSrc:$r('app.media.failed'),
|
||||||
|
onLoadListener:{
|
||||||
|
onLoadFailed(err,request){
|
||||||
|
NetWatchState.requestList.add(request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).width(200).height(200).margin({top:10})
|
||||||
|
}.width("100%")
|
||||||
|
.height("100%")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class NetWatchState {
|
||||||
|
public static netConnection: connection.NetConnection | undefined = undefined
|
||||||
|
public static requestList: List<ImageKnifeRequest> = new List()
|
||||||
|
|
||||||
|
static init() {
|
||||||
|
NetWatchState.netConnection = connection.createNetConnection()
|
||||||
|
// 注册订阅事件
|
||||||
|
NetWatchState.netConnection.register(()=>{
|
||||||
|
})
|
||||||
|
// 订阅网络能力变化事件。调用register后,才能接收到此事件通知
|
||||||
|
NetWatchState.netConnection.on("netCapabilitiesChange",(data:connection.NetCapabilityInfo)=>{
|
||||||
|
if(NetWatchState.requestList.length > 0 && data.netHandle.netId >= 100) {
|
||||||
|
NetWatchState.requestList.forEach((request)=>{
|
||||||
|
ImageKnife.getInstance().reload(request)
|
||||||
|
NetWatchState.requestList.remove(request)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
static cancel(){
|
||||||
|
if(NetWatchState.netConnection != undefined) {
|
||||||
|
// 取消订阅
|
||||||
|
NetWatchState.netConnection.unregister(()=>{})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -35,6 +35,11 @@ struct Index {
|
||||||
uri: 'pages/ImageAnimatorPage',
|
uri: 'pages/ImageAnimatorPage',
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
Button("网络恢复reload").margin({top:10}).onClick(()=>{
|
||||||
|
router.push({
|
||||||
|
uri: 'pages/ImageKnifeReload',
|
||||||
|
});
|
||||||
|
})
|
||||||
Button($r('app.string.Test_multiple_images')).margin({top:10}).onClick(()=>{
|
Button($r('app.string.Test_multiple_images')).margin({top:10}).onClick(()=>{
|
||||||
router.push({
|
router.push({
|
||||||
uri: 'pages/TestCommonImage',
|
uri: 'pages/TestCommonImage',
|
||||||
|
|
|
@ -43,6 +43,15 @@
|
||||||
],
|
],
|
||||||
"when": "always"
|
"when": "always"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "ohos.permission.GET_NETWORK_INFO",
|
||||||
|
"usedScene": {
|
||||||
|
"abilities": [
|
||||||
|
"EntryAbility"
|
||||||
|
],
|
||||||
|
"when": "always"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
"pages/ImageAnimatorPage",
|
"pages/ImageAnimatorPage",
|
||||||
"pages/TestSetCustomImagePage",
|
"pages/TestSetCustomImagePage",
|
||||||
"pages/TestErrorHolderPage",
|
"pages/TestErrorHolderPage",
|
||||||
"pages/TestTaskResourcePage"
|
"pages/TestTaskResourcePage",
|
||||||
|
"pages/ImageKnifeReload"
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -12,7 +12,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import { ImageKnifeRequest } from './model/ImageKnifeRequest';
|
import { ImageKnifeRequest, ImageKnifeRequestState } from './model/ImageKnifeRequest';
|
||||||
import { CacheStrategy, ImageKnifeData, ImageKnifeRequestSource } from './model/ImageKnifeData';
|
import { CacheStrategy, ImageKnifeData, ImageKnifeRequestSource } from './model/ImageKnifeData';
|
||||||
import { MemoryLruCache } from './cache/MemoryLruCache';
|
import { MemoryLruCache } from './cache/MemoryLruCache';
|
||||||
import { IMemoryCache } from './cache/IMemoryCache'
|
import { IMemoryCache } from './cache/IMemoryCache'
|
||||||
|
@ -80,7 +80,15 @@ export class ImageKnife {
|
||||||
public isFileCacheInit(): boolean {
|
public isFileCacheInit(): boolean {
|
||||||
return this.fileCache === undefined ? false : this.fileCache.isFileCacheInit()
|
return this.fileCache === undefined ? false : this.fileCache.isFileCacheInit()
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 重新加载
|
||||||
|
*/
|
||||||
|
reload(request: ImageKnifeRequest) {
|
||||||
|
if (request.requestState == ImageKnifeRequestState.ERROR) {
|
||||||
|
request.requestState = ImageKnifeRequestState.PROGRESS
|
||||||
|
ImageKnife.getInstance().execute(request)
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 全局添加单个请求头header
|
* 全局添加单个请求头header
|
||||||
* @param key 请求头属性
|
* @param key 请求头属性
|
||||||
|
|
|
@ -260,7 +260,7 @@ export class ImageKnifeDispatcher {
|
||||||
if (requestWithSource.source === ImageKnifeRequestSource.SRC &&
|
if (requestWithSource.source === ImageKnifeRequestSource.SRC &&
|
||||||
requestWithSource.request.imageKnifeOption.onLoadListener?.onLoadFailed !== undefined &&
|
requestWithSource.request.imageKnifeOption.onLoadListener?.onLoadFailed !== undefined &&
|
||||||
requestJobResult.loadFail) {
|
requestJobResult.loadFail) {
|
||||||
requestWithSource.request.imageKnifeOption.onLoadListener.onLoadFailed(requestJobResult.loadFail);
|
requestWithSource.request.imageKnifeOption.onLoadListener.onLoadFailed(requestJobResult.loadFail,requestWithSource.request);
|
||||||
LogUtil.log("ImageKnife_DataTime_getAndShowImage_onLoadFailed:"+currentRequest.imageKnifeOption.loadSrc)
|
LogUtil.log("ImageKnife_DataTime_getAndShowImage_onLoadFailed:"+currentRequest.imageKnifeOption.loadSrc)
|
||||||
}
|
}
|
||||||
if (requestWithSource.source === ImageKnifeRequestSource.SRC &&
|
if (requestWithSource.source === ImageKnifeRequestSource.SRC &&
|
||||||
|
|
|
@ -17,6 +17,7 @@ import common from '@ohos.app.ability.common'
|
||||||
import { CacheStrategy, ImageKnifeData,EventImage } from './ImageKnifeData';
|
import { CacheStrategy, ImageKnifeData,EventImage } from './ImageKnifeData';
|
||||||
import { PixelMapTransformation } from '../transform/PixelMapTransformation';
|
import { PixelMapTransformation } from '../transform/PixelMapTransformation';
|
||||||
import { drawing } from '@kit.ArkGraphics2D';
|
import { drawing } from '@kit.ArkGraphics2D';
|
||||||
|
import { ImageKnifeRequest } from './ImageKnifeRequest';
|
||||||
|
|
||||||
export interface HeaderOptions {
|
export interface HeaderOptions {
|
||||||
key: string;
|
key: string;
|
||||||
|
@ -89,7 +90,7 @@ export interface OnLoadCallBack {
|
||||||
onLoadSuccess?: (data: string | PixelMap | undefined, imageKnifeData: ImageKnifeData) => void;
|
onLoadSuccess?: (data: string | PixelMap | undefined, imageKnifeData: ImageKnifeData) => void;
|
||||||
|
|
||||||
// 请求结束
|
// 请求结束
|
||||||
onLoadFailed?: (err: string) => void;
|
onLoadFailed?: (err: string,request?: ImageKnifeRequest) => void;
|
||||||
// 请求取消
|
// 请求取消
|
||||||
onLoadCancel?: (reason: string) => void;
|
onLoadCancel?: (reason: string) => void;
|
||||||
}
|
}
|
Binary file not shown.
Before Width: | Height: | Size: 10 KiB |
Loading…
Reference in New Issue