ImageKnife/entry/src/main/ets/pages/testGifLoadWithWorkerPage.ets

129 lines
4.2 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 { ImageKnife,ImageKnifeGlobal, ImageKnifeComponent, ImageKnifeOption } from '@ohos/imageknife'
import worker, { MessageEvents } from '@ohos.worker'
import Prompt from '@system.prompt'
@Entry
@Component
struct TestGifLoadWithWorkerPage {
@State message: string = 'Hello Worker'
@State options: ImageKnifeOption = {
loadSrc: $r('app.media.icon')
}
private my_worker?: worker.ThreadWorker = undefined;
private my_gif_worker?: worker.ThreadWorker = undefined;
/**
* 界面进入时回调
*/
aboutToAppear() {
console.log("aboutToAppear")
}
/**
* 界面退出时回调
*/
aboutToDisappear() {
console.log("aboutToDisappear")
if (this.my_worker) {
this.my_worker.terminate()
}
if (this.my_gif_worker) {
this.my_gif_worker.terminate()
}
}
build() {
Column() {
Column() {
Text(`${this.message}`)
.fontSize("50fp")
.fontWeight(FontWeight.Bold)
Button('发worker消息')
.margin({ top: 16 })
.onClick(() => {
console.log("button 发worker消息 onClick()")
//脚本路径不要添加src/main
if (!this.my_worker) {
this.my_worker = new worker.ThreadWorker('entry/ets/workers/MyWorker.ts')
}
this.my_worker.onmessage = (e: MessageEvents) => {
console.log("my_worker.onmessage" + e.data)
//接收子线程传来的消息
//将从后台线程返回的数据通过toast的方式显示出来
Prompt.showToast({ message: e.data })
}
//主线程向子线程发送消息
this.my_worker.postMessage('好好学习')
})
Text("向子线程发送【好好学习】,并从其得到【天天向上】的应答")
Button('加载gif')
.margin({ top: 16 })
.onClick(() => {
console.log("button 加载gif onClick()")
//初始化自定义worker
if (!this.my_gif_worker) {
this.my_gif_worker = new worker.ThreadWorker('entry/ets/workers/GifLoadWorker.ts')
}
this.my_gif_worker.onmessage = (e: MessageEvents) => {
console.log("my_gif_worker.onmessage: " + e.data)
}
(ImageKnifeGlobal.getInstance().getImageKnife())?.setGifWorker(this.my_gif_worker)
//子线程加载gif,不阻塞toast的消失
this.options = {
loadSrc: $r('app.media.gifSample'),
}
Prompt.showToast({ message: '加载gif中,请稍等' })
})
Text('worker子线程解析gif不阻塞主线程中toast的消失解析耗时近45s请耐心等待')
Button('加载gif')
.margin({ top: 16 })
.onClick(() => {
let imageKnife:ImageKnife|undefined = ImageKnifeGlobal.getInstance().getImageKnife();
if(imageKnife != undefined) {
imageKnife.setGifWorker(undefined)
}
//主线程加载gif,阻塞toast的消失
this.options = {
loadSrc: $r("app.media.test"),
}
Prompt.showToast({ message: '加载gif中,请稍等' })
})
Text('主线程解析gif会阻塞主线程中toast的消失并可能会触发THREAD_BLOCK_6S异常解析耗时近20s请耐心等待')
ImageKnifeComponent({ imageKnifeOption: this.options })
.width(300)
.height(300)
.margin(16)
}
.width("100%")
.height("100%")
.justifyContent(FlexAlign.Center)
}
.width("100%")
.height("100%")
}
}