ImageKnife/entry/src/main/ets/MainAbility/glide/RequestOption.ets

295 lines
8.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (C) 2021 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 {DiskStrategy} from "../cache/diskstrategy/DiskStrategy"
import {AsyncCallback} from "../glide/interface/asynccallback.ets"
import {AsyncSuccess} from "../glide/interface/AsyncSuccess.ets"
import {AllCacheInfo, IAllCacheInfoCallback} from "../glide/interface/iallcacheinfocallback.ets"
import {AUTOMATIC} from "../cache/diskstrategy/enum/AUTOMATIC"
import {BaseTransform} from "../glide/transform/BaseTransform.ets"
import {RotateImageTransformation} from "../glide/transform/RotateImageTransformation.ets"
import {GlideData} from "../glide/GlideData.ets"
import fileio from '@ohos.fileio';
import image from '@ohos.multimedia.image';
import {CenterCrop} from '../glide/transform/pixelmap/CenterCrop.ets'
import {CenterInside} from '../glide/transform/pixelmap/CenterInside.ets'
import {FitCenter} from '../glide/transform/pixelmap/FitCenter.ets'
export class RequestOption {
loadSrc: string | PixelMap | Resource;
strategy: DiskStrategy = new AUTOMATIC();
dontAnimateFlag = false;
placeholderSrc: PixelMap | Resource;
placeholderFunc: AsyncSuccess<GlideData>;
errorholderSrc: PixelMap | Resource;
errorholderFunc: AsyncSuccess<GlideData>;
errorholderData: GlideData;
thumbSizeMultiplier: number;
// 如果存在缩略图则主图延时3000ms加载
thumbDelayTime: number = 3000
thumbholderFunc: AsyncSuccess<GlideData>;
requestListeners: Array<AsyncCallback<GlideData>>;
// 进度条
progressFunc: AsyncSuccess<string>;
// 重试图层
retryFunc: AsyncSuccess<GlideData>
// 图层切换时长
animateDuraction: number = 500;
size: {
width: number,
height: number
} = { width: -1, height: -1 };
// 网络下载数据回调
allCacheInfoCallback: IAllCacheInfoCallback;
onlyRetrieveFromCache: boolean = false;
isCacheable: boolean = true;
// 变换相关
transformtions: Array<BaseTransform<PixelMap>> = new Array();
generateCacheKey: string = "";
generateResourceKey: string = "";
generateDataKey: string = "";
private filesPath: string = ""; // data/data/包名/files目录
private cachesPath: string = ""; // 网络下载默认存储在data/data/包名/cache/GlideNetworkFolder/目标md5.img下面
// 下载原始文件地址
downloadFilePath: string = "";
// 网络文件下载统一存放
networkCacheFolder: string = "GlideNetworkFolder"
// 主线图片 状态变化 是否加载完成
// 主图未加载成功 显示占位图 主图加载成功不展示占位图
loadMainReady = false;
// 失败占位图展示状态 当true 表示主图加载失败需要展示失败占位图
loadErrorReady = false
// 缩略图展示
loadThumbnailReady = false;
_svgAndGifFolder: string = "svgAndGifFolder"; // svg和gif的文件路径地址
_svgAndGifCommitFile: string = "svgAndGifCommitFile"; // svg和gif提交记录
constructor() {
// 初始化全局监听
this.requestListeners = new Array();
}
/**
* set image Component size
*/
setImageViewSize(imageSize: {
width: number,
height: number
}) {
this.size.width = imageSize.width;
this.size.height = imageSize.height;
return this;
}
getFilesPath() {
return this.filesPath;
}
setFilesPath(path: string) {
this.filesPath = path;
}
getCachesPath() {
return this.cachesPath;
}
setCachesPath(path: string) {
this.cachesPath = path;
}
load(src: string | PixelMap | Resource) {
this.loadSrc = src;
return this;
}
diskCacheStrategy(strategy: DiskStrategy) {
this.strategy = strategy;
return this;
}
dontAnimate() {
this.dontAnimateFlag = true;
return this;
}
// 仅支持 本地图片
placeholder(src: PixelMap | Resource, func?: AsyncSuccess<GlideData>) {
this.placeholderSrc = src;
this.placeholderFunc = func;
return this;
}
errorholder(src: PixelMap | Resource, func?: AsyncSuccess<GlideData>) {
this.errorholderSrc = src;
this.errorholderFunc = func;
return this;
}
thumbnail(sizeMultiplier: number, func?: AsyncSuccess<GlideData>) {
this.thumbSizeMultiplier = sizeMultiplier;
this.thumbholderFunc = func;
return this;
}
addProgressListener(func?: AsyncSuccess<string>) {
this.progressFunc = func;
return this;
}
addRetryListener(func?: AsyncSuccess<any>) {
this.retryFunc = func;
return this;
}
addListener(func: AsyncCallback<GlideData>) {
this.requestListeners.push(func);
return this;
}
addAllCacheInfoCallback(func: IAllCacheInfoCallback) {
this.allCacheInfoCallback = func;
return this;
}
skipMemoryCache(skip: boolean) {
this.isCacheable = !skip;
return this;
}
retrieveDataFromCache(flag: boolean) {
this.onlyRetrieveFromCache = flag;
}
rotateImage(degreesToRotate: number) {
let rotateImage = new RotateImageTransformation(degreesToRotate);
this.transformtions.push(rotateImage);
return this;
}
centerCrop() {
this.transformtions.push(new CenterCrop());
return this;
}
centerInside() {
this.transformtions.push(new CenterInside());
return this;
}
fitCenter() {
this.transformtions.push(new FitCenter());
return this;
}
transform(transform: BaseTransform<PixelMap>) {
this.transformtions.push(transform);
return this;
}
transforms(transforms: BaseTransform<PixelMap>[]) {
this.transformtions = transforms;
return this;
}
// 占位图解析成功
placeholderOnComplete(glidedata: GlideData) {
console.log("placeholderOnComplete has called!");
console.log("Main Image is Ready:" + this.loadMainReady);
if (!this.loadMainReady && !this.loadErrorReady && !this.loadThumbnailReady) {
// 主图未加载成功,并且未加载失败 显示占位图 主图加载成功或者加载失败后=>不展示占位图
this.placeholderFunc(glidedata)
}
}
// 占位图解析失败
placeholderOnError(error) {
console.log("占位图解析失败 error =" + error)
}
// 缩略图解析成功
thumbholderOnComplete(glidedata: GlideData) {
if (!this.loadMainReady && !this.loadErrorReady) {
//主图未加载成功,并且未加载失败 显示占位图 主图加载成功或者加载失败后=>不展示占位图
this.thumbholderFunc(glidedata)
}
}
// 缩略图解析失败
thumbholderOnError(error) {
console.log("缩略图解析失败 error =" + error)
}
// 加载失败 占位图解析成功
errorholderOnComplete(glidedata: GlideData) {
// 如果有错误占位图 先解析并保存在RequestOption中 等到加载失败时候进行调用
this.errorholderData = glidedata;
if (this.loadErrorReady) {
this.errorholderFunc(glidedata)
}
}
//加载失败 占位图解析失败
errorholderOnError(error) {
console.log("失败占位图解析失败 error =" + error)
}
loadComplete(glidedata: GlideData) {
this.loadMainReady = true;
// 三级缓存数据加载成功
for (let requestListener of this.requestListeners) {
var ret = requestListener("", glidedata);
if (ret) {
break;
}
}
// 加载成功之后
Glide.removeRunning(this);
}
loadError(err) {
console.log("loadError:"+err);
//失败占位图展示规则
this.loadErrorReady = true;
if (this.retryFunc) {
// 重试图层优先于加载失败展示
this.retryFunc(err)
} else {
if (this.errorholderData != null) {
this.errorholderFunc(this.errorholderData)
}
}
// 加载失败之后
Glide.removeRunning(this);
}
}
var Glide;
var defaultTemp = globalThis.exports.default
if (defaultTemp != undefined) {
Glide = defaultTemp.data.glide;
}