ImageKnife/entry/src/main/ets/MainAbility/pages/storageTestDiskLruCache.ets

325 lines
12 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 {GlideOption} from "../glide/GlideOption"
import {RequestOption} from "../glide/RequestOption"
import {GlideData} from "../glide/GlideData"
import {PixelMapPack} from "../glide/PixelMapPack"
@Entry
@Component
struct StorageTestDiskLruCache {
@Watch('watchGlideOption') @State glideOption: GlideOption =
{
loadSrc: $r('app.media.jpgSample'),
size: { width: 300, height: 300 },
placeholderSrc: $r('app.media.icon_loading'),
errorholderSrc: $r('app.media.icon_failed'),
};
@State glidePixelMapPack: PixelMapPack = new PixelMapPack();
@State glideResource: Resource = $r('app.media.icon_loading')
@State glideString: string = ""
@State normalPixelMap: boolean = false;
@State normalResource: boolean = true;
previousData: GlideData = null;
nowData: GlideData = null;
@State logText: string = "打印日志结果";
watchGlideOption() {
this.glideExecute();
}
build() {
Scroll() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text("Glide测试DiskLruCache能力")
.backgroundColor(Color.Blue)
Button("替换Glide默认DiskLruCache并设置大小1M")
.onClick(() => {
diskGlide.replaceDiskLruCache(1 * 1024 * 1024)
}).margin({top:15, bottom:15})
Button("替换Glide默认DiskLruCache并设置大小30M")
.onClick(() => {
diskGlide.replaceDiskLruCache(30 * 1024 * 1024)
}).margin({top:15, bottom:15})
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button("加载jpg网络图")
.onClick(() => {
this.glideOption = {
loadSrc: "https://hbimg.huabanimg.com/cc6af25f8d782d3cf3122bef4e61571378271145735e9-vEVggB",
size: { width: 300, height: 300 },
placeholderSrc: $r('app.media.icon_loading'),
errorholderSrc: $r('app.media.icon_failed'),
margin:{left:5,top:5,right:5,bottom:5}
};
}).margin({ top: 5, left: 10 })
Button("加载png网络图")
.onClick(() => {
this.glideOption = {
loadSrc: "https://img-blog.csdnimg.cn/20191215043500229.png",
size: { width: 300, height: 300 },
placeholderSrc: $r('app.media.icon_loading'),
errorholderSrc: $r('app.media.icon_failed'),
margin:{left:5,top:5,right:5,bottom:5}
};
}).margin({ top: 5, left: 10 })
Button("加载webp网络图")
.onClick(() => {
this.glideOption = {
loadSrc: "https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp",
size: { width: 300, height: 300 },
placeholderSrc: $r('app.media.icon_loading'),
errorholderSrc: $r('app.media.icon_failed'),
margin:{left:5,top:5,right:5,bottom:5}
};
}).margin({ top: 5, left: 10 })
Button("加载bmp网络图")
.onClick(() => {
this.glideOption = {
loadSrc: "https://img-blog.csdn.net/20140514114029140",
size: { width: 300, height: 300 },
placeholderSrc: $r('app.media.icon_loading'),
errorholderSrc: $r('app.media.icon_failed'),
margin:{left:5,top:5,right:5,bottom:5}
};
}).margin({ top: 5, left: 10 })
}.width('100%')
.height(60).backgroundColor(Color.Pink)
Image(this.normalPixelMap ? this.glidePixelMapPack.pixelMap : (this.normalResource ? this.glideResource : this.glideString))
.width(this.glideOption.size ? this.glideOption.size.width : '100%')
.height(this.glideOption.size ? this.glideOption.size.height : '100%')
.objectFit(this.glideOption.imageFit ? this.glideOption.imageFit : ImageFit.Fill)
.backgroundColor(this.glideOption.backgroundColor ? this.glideOption.backgroundColor : Color.White)
.margin(this.glideOption.margin ? this.glideOption.margin : { left: 0, top: 0, right: 0, bottom: 0 })
Scroll() {
Text(this.logText).fontSize(15)
}.width(300).height(300).margin({top:20}).backgroundColor(Color.Pink)
}
}
.width('100%')
.height('100%')
}
// glide 第一次启动和数据刷新后重新发送请求
glideExecute() {
let request = new RequestOption();
request.load(this.glideOption.loadSrc)
.addListener((err, data) => {
this.glideChangeSource(data)
return false;
})
if (this.glideOption.placeholderSrc) {
request.placeholder(this.glideOption.placeholderSrc, (data) => {
this.glideChangeSource(data)
})
}
if (this.glideOption.errorholderSrc) {
request.errorholder(this.glideOption.errorholderSrc, (data) => {
this.glideChangeSource(data)
})
}
if (this.glideOption.transformtions) {
request.transforms(this.glideOption.transformtions)
}
if (this.glideOption.size) {
request.setImageViewSize(this.glideOption.size)
}
if (this.glideOption.onlyRetrieveFromCache) {
request.retrieveDataFromCache(this.glideOption.onlyRetrieveFromCache)
}
if (this.glideOption.isCacheable) {
request.skipMemoryCache(!this.glideOption.isCacheable)
}
if (this.glideOption.strategy) {
request.diskCacheStrategy(this.glideOption.strategy)
}
if (this.glideOption.dontAnimateFlag) {
request.dontAnimate()
}
if (this.glideOption.allCacheInfoCallback) {
request.addAllCacheInfoCallback(this.glideOption.allCacheInfoCallback)
}
diskGlide.call(request);
}
glideChangeSource(data:GlideData) {
this.glideSpecialFixed(data);
//查看mGlide中的DiskLruCache
let disk = diskGlide.getDiskMemoryCache();
let showDisk = ''
disk.foreachDiskLruCache((value, key, map) => {
showDisk += "key=" + key + "&value=" + value;
})
this.logText = "日志结果:" + showDisk;
}
glideSpecialFixed(data:GlideData) {
if (this.nowData) {
this.previousData = this.nowData;
this.nowData = data;
if (data.isPixelMap()) {
// PixelMap
console.log("Glide占位图输出=PixelMap")
if (this.previousData.isSvg()) {
console.log("Glide占位图输出=PixelMap 上一个是SVG")
this.normalPixelMap = true;
this.normalResource = true;
let pixelMapPack1 = new PixelMapPack();
pixelMapPack1.pixelMap = new PixelMap();
this.glidePixelMapPack = pixelMapPack1;
setTimeout(() => {
let pixelMapPack2 = new PixelMapPack();
pixelMapPack2.pixelMap = data.glideValue as PixelMap;
this.glidePixelMapPack = pixelMapPack2;
}, 100)
} else {
this.normalPixelMap = true;
this.normalResource = true;
let pixelMapPack3 = new PixelMapPack();
pixelMapPack3.pixelMap = data.glideValue as PixelMap;
this.glidePixelMapPack = pixelMapPack3;
}
}
else if (data.isString()) {
// String
console.log("glidevalue=" + JSON.stringify(data));
console.log("Glide占位图输出=String")
if (this.previousData.isSvg()) {
console.log("data.glideValue=" + JSON.stringify(data.glideValue))
console.log("Glide占位图输出=String 拥有上一个图片类型 上一个是SVG")
this.normalPixelMap = false;
this.normalResource = false;
let firstIndex = (data.glideValue as string).indexOf(diskGlide.getSvgAndGifFolder());
console.log("firstIndex=" + firstIndex);
let suffix = (data.glideValue as string).substring(firstIndex, (data.glideValue as string).length)
console.log("suffix =" + suffix);
let glideNeedStr = 'internal://app/' + suffix;
this.glideString = glideNeedStr;
} else {
console.log("data.glideValue=" + JSON.stringify(data.glideValue))
console.log("Glide占位图输出=String 拥有上一个图片类型 上一个不是SVG")
this.normalPixelMap = false;
this.normalResource = false;
let firstIndex = (data.glideValue as string).indexOf(diskGlide.getSvgAndGifFolder());
console.log("firstIndex=" + firstIndex);
let suffix = (data.glideValue as string).substring(firstIndex, (data.glideValue as string).length)
console.log("suffix =" + suffix);
let glideNeedStr = 'internal://app/' + suffix;
this.glideString = glideNeedStr;
}
} else if (data.isResource()) {
console.log("Glide占位图输出=Resource")
if (this.previousData.isSvg()) {
console.log("Glide占位图输出=Resource 上一个是SVG")
this.normalPixelMap = false;
this.normalResource = true;
this.glideResource = data.glideValue as Resource;
} else {
this.normalPixelMap = false;
this.normalResource = true;
this.glideResource = data.glideValue as Resource;
}
} else {
console.log("Glide占位图输出=数据错误")
}
} else {
this.nowData = data;
if (data.isPixelMap()) {
// PixelMap
console.log("Glide占位图输出=PixelMap")
this.normalPixelMap = true;
this.normalResource = true;
let pixelMapPack4 = new PixelMapPack();
pixelMapPack4.pixelMap = data.glideValue as PixelMap;
this.glidePixelMapPack = pixelMapPack4;
}
else if (data.isString()) {
// String
console.log("data.glideValue=" + JSON.stringify(data.glideValue))
console.log("Glide占位图输出=String 没有上一个图片类型")
this.normalPixelMap = false;
this.normalResource = false;
let firstIndex = (data.glideValue as string).indexOf(diskGlide.getSvgAndGifFolder());
console.log("firstIndex=" + firstIndex);
let suffix = (data.glideValue as string).substring(firstIndex, (data.glideValue as string).length)
console.log("suffix =" + suffix);
let glideNeedStr = 'internal://app/' + suffix;
this.glideString = glideNeedStr;
} else if (data.isResource()) {
console.log("Glide占位图输出=Resource")
this.normalPixelMap = false;
this.normalResource = true;
this.glideResource = data.glideValue as Resource;
} else {
console.log("Glide占位图输出=数据错误")
}
}
}
//函数在自定义组件析构消耗之前执行。
//不允许在aboutToDisappear函数中改变状态变量特别是@Link变量的修改可能会导致应用程序行为不稳定。
aboutToDisappear() {
}
//当此页面显示时触发一次。包括路由过程、应用进入前后台等场景,仅@Entry修饰的自定义组件生效。
onPageShow() {
}
//当此页面消失时触发一次。包括路由过程、应用进入前后台等场景,仅@Entry修饰的自定义组件生效。
onPageHide() {
}
// 当用户点击返回按钮时触发,,仅@Entry修饰的自定义组件生效。
//返回true表示页面自己处理返回逻辑, 不进行页面路由。
//返回false表示使用默认的返回逻辑。
//不返回值会作为false处理。
onBackPress() {
}
}
var diskGlide = globalThis.exports.default.data.glide