1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import router from '@ohos.router'; 17import worker from '@ohos.worker'; 18import { IconItem } from './IconView'; 19import { IconItemSource } from './IconItemSource'; 20 21 22class PicData { 23 public iconItemSourceList: IconItemSource[] = []; 24 25 public setUp(): string { 26 for (let index = 0; index < 20; index++) { 27 const numStart: number = index * 6; 28 // 此处循环使用6张图片资源 29 this.iconItemSourceList.push(new IconItemSource($r('app.media.nearby'), `item${numStart + 1}`)); 30 this.iconItemSourceList.push(new IconItemSource($r('app.media.scan'), `item${numStart + 2}`)); 31 this.iconItemSourceList.push(new IconItemSource($r('app.media.shop'), `item${numStart + 3}`)); 32 this.iconItemSourceList.push(new IconItemSource($r('app.media.cards'), `item${numStart + 4}`)); 33 this.iconItemSourceList.push(new IconItemSource($r('app.media.album'), `item${numStart + 5}`)); 34 this.iconItemSourceList.push(new IconItemSource($r('app.media.applet'), `item${numStart + 6}`)); 35 } 36 return "setUpIconItemSourceList success!"; 37 } 38} 39 40@Component 41export struct WorkerCallGlobalUsage { 42 @State iconItemSourceList: IconItemSource[] = []; 43 private workerInstance: worker.ThreadWorker | undefined = undefined; 44 private message: string = "run setUp in picData"; 45 46 aboutToAppear() { 47 this.initWorker(); 48 } 49 50 build() { 51 Column() { 52 // 顶部导航栏 53 Row() { 54 Image($r("app.media.back")) 55 .height(24) 56 .width(24) 57 .onClick(() => { 58 router.back(); 59 }) 60 61 Text($r('app.string.scenario_5')) 62 .fontSize(24) 63 .lineHeight(30) 64 .fontColor(Color.Black) 65 .fontWeight(FontWeight.Bold) 66 .textAlign(TextAlign.Start) 67 .margin({ 68 left: 16 69 }) 70 } 71 .width($r('app.string.percent_100')) 72 .height(50) 73 .justifyContent(FlexAlign.Start) 74 .padding({ 75 left: 24 76 }) 77 78 Button($r('app.string.load_image'), { type: ButtonType.Normal, stateEffect: true }) 79 .fontSize(14) 80 .borderRadius(8) 81 .backgroundColor(Color.Blue) 82 .width(250) 83 .height(60) 84 .margin({ 85 top: 30 86 }) 87 .onClick(() => { 88 if (this.workerInstance !== undefined) { 89 this.workerInstance.postMessage(this.message); 90 } 91 }) 92 93 if (this.iconItemSourceList.length > 0) { 94 List({ space: 20 }) { 95 ForEach(this.iconItemSourceList, (item: IconItemSource) => { 96 ListItem() { 97 IconItem({ image: item.image, text: item.text }); 98 } 99 }, (item: IconItemSource, index) => index.toString()) 100 } 101 .divider({ strokeWidth: 2, startMargin: 20, endMargin: 20 }) // 每行之间的分界线 102 .width($r('app.string.percent_100')) 103 .height($r('app.string.percent_100')) 104 .layoutWeight(1) 105 } 106 } 107 .width($r('app.string.percent_100')) 108 .height($r('app.string.percent_100')) 109 .alignItems(HorizontalAlign.Center) 110 } 111 112 initWorker() { 113 if (this.workerInstance !== undefined) { 114 let picData: PicData = new PicData(); 115 picData.iconItemSourceList = this.iconItemSourceList; 116 // 在ThreadWorker实例上注册registerObj 117 this.workerInstance.registerGlobalCallObject("picData", picData); 118 } 119 } 120}