From 84c099269043cc5775d5b13e57ee86a991d3bba4 Mon Sep 17 00:00:00 2001 From: xinhu <1216197016@qq.com> Date: Sat, 9 Sep 2023 18:10:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?arkTS=E8=AF=AD=E6=B3=95=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xinhu <1216197016@qq.com> --- RoundedImageView/index.ets | 2 +- .../src/main/ets/components/FileUtils.ts | 2 +- .../src/main/ets/components/PixelMapUtils.ts | 12 +- .../main/ets/components/RoundedImageView.ets | 146 ++++++++++-------- .../src/main/ets/entryability/EntryAbility.ts | 19 ++- .../main/ets/entryability/GlobalContext.ts | 22 +++ entry/src/main/ets/pages/Index.ets | 108 ++++++------- entry/src/main/ets/pages/PictureItem.ets | 4 +- entry/src/main/ets/pages/TypeCustomDialog.ets | 8 +- 9 files changed, 184 insertions(+), 139 deletions(-) create mode 100644 entry/src/main/ets/entryability/GlobalContext.ts diff --git a/RoundedImageView/index.ets b/RoundedImageView/index.ets index 50b4fa9..7282bdb 100644 --- a/RoundedImageView/index.ets +++ b/RoundedImageView/index.ets @@ -13,7 +13,7 @@ * limitations under the License. */ -export { default as RoundedImageView } from './src/main/ets/components/RoundedImageView' +export { RoundedImageName,RoundedImageView } from './src/main/ets/components/RoundedImageView' export { default as ScaleType } from './src/main/ets/components/ScaleType' diff --git a/RoundedImageView/src/main/ets/components/FileUtils.ts b/RoundedImageView/src/main/ets/components/FileUtils.ts index 080bbff..2b9805d 100644 --- a/RoundedImageView/src/main/ets/components/FileUtils.ts +++ b/RoundedImageView/src/main/ets/components/FileUtils.ts @@ -58,7 +58,7 @@ class FileUtils { /** * 创建文件夹 */ - createFolder(path: string) { + createFolder(path: string):void { //创建文件夹 if (!this.existFolder(path)) { fileio.mkdirSync(path) diff --git a/RoundedImageView/src/main/ets/components/PixelMapUtils.ts b/RoundedImageView/src/main/ets/components/PixelMapUtils.ts index d534c6d..6bd31aa 100644 --- a/RoundedImageView/src/main/ets/components/PixelMapUtils.ts +++ b/RoundedImageView/src/main/ets/components/PixelMapUtils.ts @@ -17,12 +17,15 @@ import image from '@ohos.multimedia.image'; import SrcType from './srctype'; import DownloadUtils from './downloadutils'; +import { GlobalContext } from '../../../../../entry/src/main/ets/entryability/GlobalContext' +import common from '@ohos.app.ability.common'; class PixelMapUtils { - public static createPixelMap(src: string | Resource | ArrayBuffer, srcType: SrcType, callback: (pixelMap: image.PixelMap, width: number, height: number, error?: Error) => void): void { + public static createPixelMap(src: string | Resource | ArrayBuffer, srcType: SrcType | null, callback: (pixelMap: image.PixelMap, width: number, height: number, error?: Error) => void): void { + let context:Context = GlobalContext.getContext().getObject("context") as Context switch (srcType) { case SrcType.MEDIA: //media相关数据 - globalThis.resourceManager.getMediaContent(src.id, (error, value) => { + context.resourceManager.getMediaContent(src.id, (error, value) => { if (error != null) { console.log("error is " + error); } else if(value){ @@ -31,7 +34,7 @@ class PixelMapUtils { }) break case SrcType.RAWFILE: //本地rawfile相关数据 - globalThis.resourceManager.getRawFileContent(src, (error, value) => { + context.resourceManager.getRawFileContent(src, (error, value) => { if (error != null) { console.log("error is " + error); } else if(value){ @@ -43,10 +46,9 @@ class PixelMapUtils { this.callbackPixelMap(src, callback) break case SrcType.URL: //url相关数据 - let that = this new DownloadUtils().loadData(src, (arrayBuffer, error) => { if (!error) { - that.callbackPixelMap(arrayBuffer, callback) + this.callbackPixelMap(arrayBuffer, callback) } }) break diff --git a/RoundedImageView/src/main/ets/components/RoundedImageView.ets b/RoundedImageView/src/main/ets/components/RoundedImageView.ets index 643417c..16d274e 100644 --- a/RoundedImageView/src/main/ets/components/RoundedImageView.ets +++ b/RoundedImageView/src/main/ets/components/RoundedImageView.ets @@ -18,19 +18,20 @@ import PixelMapUtils from './pixelmaputils'; import ScaleType from './scaletype'; import TileMode from './tilemode'; import SrcType from './srctype'; +import { GlobalContext } from '../../../../../entry/src/main/ets/entryability/GlobalContext' @Component -struct RoundedImageView { - @State model: RoundedImageView.Model = new RoundedImageView.Model() +export struct RoundedImageView { + @State model: RoundedImageName.Model = new RoundedImageName.Model() private settings: RenderingContextSettings = new RenderingContextSettings(true) private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) private bottomLayerContext: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) - private img: PixelMap - private srcWidth: number - private srcHeight: number + private img: PixelMap | null = null + private srcWidth: number = 0; + private srcHeight: number = 0; private minScale: number = 1 - private scaleW: number - private scaleH: number + private scaleW: number = 0; + private scaleH: number = 0; private svgImageViewModel: SVGImageView.SVGImageViewModel = new SVGImageView.SVGImageViewModel() public aboutToAppear() { @@ -63,33 +64,35 @@ struct RoundedImageView { } setImage() { - switch (this.model.scaleType) { - case ScaleType.FIT_START: - this.context.drawImage(this.img, 0, 0, this.srcWidth, this.srcHeight) - break - case ScaleType.FIT_END: - this.context.drawImage(this.img, (this.model.uiWidth - this.model.padding * 2 - this.srcWidth * this.minScale) / (this.minScale * 1), (this.model.uiHeight - this.model.padding * 2 - this.srcHeight * this.minScale) / (this.minScale * 1), this.srcWidth, this.srcHeight) - break - case ScaleType.FIT_CENTER: - this.context.drawImage(this.img, (this.model.uiWidth - this.model.padding * 2 - this.srcWidth * this.minScale) / (this.minScale * 2), (this.model.uiHeight - this.model.padding * 2 - this.srcHeight * this.minScale) / (this.minScale * 2), this.srcWidth, this.srcHeight) - break - case ScaleType.CENTER: - this.context.drawImage(this.img, (this.model.uiWidth - this.model.padding * 2 - this.srcWidth) / 2, (this.model.uiHeight - this.model.padding * 2 - this.srcHeight) / 2, this.srcWidth, this.srcHeight) - break - case ScaleType.CENTER_CROP: - this.context.drawImage(this.img, (this.model.uiWidth - this.model.padding * 2 - this.srcWidth * this.minScale) / (this.minScale * 2), (this.model.uiHeight - this.model.padding * 2 - this.srcHeight * this.minScale) / (this.minScale * 2), this.srcWidth, this.srcHeight) - break - case ScaleType.FIT_XY: - if (this.model.tileMode == TileMode.REPEAT || this.model.tileMode == TileMode.MIRROR) { - this.context.restore() - this.setTileMode() - } else { + if (!!this.img) { + switch (this.model.scaleType) { + case ScaleType.FIT_START: this.context.drawImage(this.img, 0, 0, this.srcWidth, this.srcHeight) - } - break - case ScaleType.CENTER_INSIDE: - this.context.drawImage(this.img, (this.model.uiWidth - this.model.padding * 2 - this.srcWidth * this.minScale) / (this.minScale * 2), (this.model.uiHeight - this.model.padding * 2 - this.srcHeight * this.minScale) / (this.minScale * 2), this.srcWidth, this.srcHeight) - break + break + case ScaleType.FIT_END: + this.context.drawImage(this.img, (this.model.uiWidth - this.model.padding * 2 - this.srcWidth * this.minScale) / (this.minScale * 1), (this.model.uiHeight - this.model.padding * 2 - this.srcHeight * this.minScale) / (this.minScale * 1), this.srcWidth, this.srcHeight) + break + case ScaleType.FIT_CENTER: + this.context.drawImage(this.img, (this.model.uiWidth - this.model.padding * 2 - this.srcWidth * this.minScale) / (this.minScale * 2), (this.model.uiHeight - this.model.padding * 2 - this.srcHeight * this.minScale) / (this.minScale * 2), this.srcWidth, this.srcHeight) + break + case ScaleType.CENTER: + this.context.drawImage(this.img, (this.model.uiWidth - this.model.padding * 2 - this.srcWidth) / 2, (this.model.uiHeight - this.model.padding * 2 - this.srcHeight) / 2, this.srcWidth, this.srcHeight) + break + case ScaleType.CENTER_CROP: + this.context.drawImage(this.img, (this.model.uiWidth - this.model.padding * 2 - this.srcWidth * this.minScale) / (this.minScale * 2), (this.model.uiHeight - this.model.padding * 2 - this.srcHeight * this.minScale) / (this.minScale * 2), this.srcWidth, this.srcHeight) + break + case ScaleType.FIT_XY: + if (this.model.tileMode == TileMode.REPEAT || this.model.tileMode == TileMode.MIRROR) { + this.context.restore() + this.setTileMode() + } else { + this.context.drawImage(this.img, 0, 0, this.srcWidth, this.srcHeight) + } + break + case ScaleType.CENTER_INSIDE: + this.context.drawImage(this.img, (this.model.uiWidth - this.model.padding * 2 - this.srcWidth * this.minScale) / (this.minScale * 2), (this.model.uiHeight - this.model.padding * 2 - this.srcHeight * this.minScale) / (this.minScale * 2), this.srcWidth, this.srcHeight) + break + } } } @@ -189,22 +192,28 @@ struct RoundedImageView { this.context.scale(-1, -1) } } - this.context.drawImage(this.img, indexW * this.srcWidth, indexH * this.srcHeight, this.srcWidth, this.srcHeight) - this.context.restore() + if(!!this.img){ + this.context.drawImage(this.img, indexW * this.srcWidth, indexH * this.srcHeight, this.srcWidth, this.srcHeight) + this.context.restore() + } } } this.context.restore() } private getSvgData(callback: (svgData: Uint8Array) => void) { + let context:Context = GlobalContext.getContext().getObject("context") as Context + switch (this.model.srcType) { case SrcType.MEDIA: - globalThis.resourceManager.getMedia(this.model.src, (error, value) => { - callback(value); - }) + let mediaSrc:Resource = this.model.src as Resource + context.resourceManager.getMediaContent(mediaSrc.id, (error:Error, value:Uint8Array) => { + callback(value); + }) break case SrcType.RAWFILE: - globalThis.resourceManager.getRawFile(this.model.src, (error, value) => { + let rawfileSrc:string = this.model.src as string + context.resourceManager.getRawFileContent(rawfileSrc, (error:Error, value:Uint8Array) => { callback(value); }) break @@ -421,7 +430,7 @@ struct RoundedImageView { .height(this.model.uiHeight) .onReady(() => { let that = this - let timeoutID = setTimeout(function () { + let timeoutID = setTimeout( () => { that.drawBitmap() clearTimeout(timeoutID) }, 100); @@ -431,28 +440,28 @@ struct RoundedImageView { } } -namespace RoundedImageView { +export namespace RoundedImageName { export class Model { - src: string | Resource | ArrayBuffer; - srcType: SrcType - isSvg: boolean - typeValue: string - uiWidth: number - uiHeight: number - backgroundColor: string | CanvasGradient | CanvasPattern - tileMode: TileMode - scaleType: ScaleType - cornerRadius: number - borderWidth: number - borderColor: string | CanvasGradient | CanvasPattern - padding: number - colorWidth: number - colorHeight: number + src: string | ArrayBuffer | Resource = '' + srcType: SrcType | null = null + isSvg: boolean = false + typeValue: string = ''; + uiWidth: number = 0; + uiHeight: number = 0; + backgroundColor: string | CanvasGradient | CanvasPattern = '' + tileMode: TileMode | null = null + scaleType: ScaleType | null = null + cornerRadius: number = 0; + borderWidth: number = 0; + borderColor: string | CanvasGradient | CanvasPattern = '' + padding: number = 0; + colorWidth: number = 0; + colorHeight: number = 0; constructor() { } - setImageSrc(src: string | Resource | ArrayBuffer): Model { + setImageSrc(src: string | ArrayBuffer | Resource): Model { this.src = src; return this; } @@ -461,13 +470,15 @@ namespace RoundedImageView { return this.src; } - setSrcType(srcType: SrcType): Model { + setSrcType(srcType: SrcType | null): Model { this.srcType = srcType; return this; } - getSrcType(): SrcType { - return this.srcType; + getSrcType(): SrcType | void { + if(this.srcType !== null){ + return this.srcType; + } } setIsSvg(isSvg: boolean): Model { @@ -511,17 +522,21 @@ namespace RoundedImageView { return this; } - getScaleType(): ScaleType { - return this.scaleType; + getScaleType(): ScaleType| void { + if(this.scaleType !== null){ + return this.scaleType; + } } - setTileModeXY(tileMode: TileMode): Model { + setTileModeXY(tileMode: TileMode | null): Model { this.tileMode = tileMode; return this; } - getTileModeXY(): TileMode { - return this.tileMode; + getTileModeXY(): TileMode | void{ + if( this.tileMode !== null){ + return this.tileMode; + } } setBackgroundColor(backgroundColor: string | CanvasGradient | CanvasPattern): Model { @@ -589,4 +604,3 @@ namespace RoundedImageView { } } -export default RoundedImageView; diff --git a/entry/src/main/ets/entryability/EntryAbility.ts b/entry/src/main/ets/entryability/EntryAbility.ts index aa6d4cb..b7b8a40 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ts +++ b/entry/src/main/ets/entryability/EntryAbility.ts @@ -17,17 +17,22 @@ import UIAbility from '@ohos.app.ability.UIAbility'; import hilog from '@ohos.hilog'; import window from '@ohos.window'; import display from '@ohos.display'; - +import { GlobalContext } from './GlobalContext' export default class EntryAbility extends UIAbility { onCreate(want, launchParam) { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); - globalThis.abilityWant = want; - globalThis.globalFilesDir = this.context.filesDir + // globalThis.abilityWant = want; let displayWH = display.getDefaultDisplaySync() - globalThis.deviceW = displayWH.width; - globalThis.deviceH = displayWH.height; - globalThis.resourceManager = this.context.resourceManager; - globalThis.context = this.context + GlobalContext.getContext().setObject("abilityWant", want); + GlobalContext.getContext().setObject("globalFilesDir", this.context.filesDir); + GlobalContext.getContext().setObject("deviceW", displayWH.width); + GlobalContext.getContext().setObject("deviceH", displayWH.height); + GlobalContext.getContext().setObject("context", this.context); + // globalThis.globalFilesDir = this.context.filesDir + // let displayWH = display.getDefaultDisplaySync() + // globalThis.deviceW = displayWH.width; + // globalThis.deviceH = displayWH.height; + // globalThis.context = this.context } onDestroy() { diff --git a/entry/src/main/ets/entryability/GlobalContext.ts b/entry/src/main/ets/entryability/GlobalContext.ts new file mode 100644 index 0000000..64b5f29 --- /dev/null +++ b/entry/src/main/ets/entryability/GlobalContext.ts @@ -0,0 +1,22 @@ +// 构造单例对象 +export class GlobalContext { + private constructor() {} + private static instance: GlobalContext; + private _objects = new Map(); + + public static getContext(): GlobalContext { + if (!GlobalContext.instance) { + GlobalContext.instance = new GlobalContext(); + } + return GlobalContext.instance; + } + + getObject(value: string):Object { + return this._objects.get(value); + } + + setObject(key: string, objectClass: Object): void { + this._objects.set(key, objectClass); + } + +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index a3a2e11..46e2890 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -15,18 +15,20 @@ import { PictureItem } from './PictureItem' import { TypeCustomDialog } from './TypeCustomDialog' -import { RoundedImageView, ScaleType, TileMode, SrcType, FileUtils } from '@ohos/roundedimageview' +import { RoundedImageView,RoundedImageName, ScaleType, TileMode, SrcType, FileUtils } from '@ohos/roundedimageview' +import { GlobalContext } from '../../../../../entry/src/main/ets/entryability/GlobalContext' @Entry @Component struct Index { private picIdxArr: number [] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] private colorIdxArr: number [] = [0, 1, 2, 3, 4, 5, 6] - private uiWidth: number = px2vp(globalThis.deviceW * 0.9) + private uiWidth: number = px2vp((GlobalContext.getContext().getObject("deviceW") as number) * 0.9) private uiHeight: number = px2vp(500) - private uriFolder: string = globalThis.globalFilesDir + "/" + "uriFolder" + + private uriFolder: string = (GlobalContext.getContext().getObject("globalFilesDir") as string) + "/" + "uriFolder" private uriFile: string = this.uriFolder + "/" + "photo1.jpg" - private viewModels: RoundedImageView.Model [] = []; + private viewModels: RoundedImageName.Model [] = []; private scroller: Scroller = new Scroller() private dialogController: CustomDialogController = new CustomDialogController({ alignment: DialogAlignment.Top, @@ -45,7 +47,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -62,7 +64,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor:'', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -79,7 +81,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -96,7 +98,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -113,7 +115,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -130,7 +132,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -147,7 +149,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -164,7 +166,7 @@ struct Index { tileMode: TileMode.REPEAT, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -181,7 +183,7 @@ struct Index { tileMode: TileMode.CLAMP, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -198,7 +200,7 @@ struct Index { tileMode: TileMode.MIRROR, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -217,7 +219,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 0, borderWidth: 10, borderColor: '#317AF7', @@ -234,7 +236,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 0, borderWidth: 10, borderColor: '#317AF7', @@ -251,7 +253,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 0, borderWidth: 10, borderColor: '#317AF7', @@ -268,7 +270,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 0, borderWidth: 10, borderColor: '#317AF7', @@ -285,7 +287,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 0, borderWidth: 10, borderColor: '#317AF7', @@ -302,7 +304,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 0, borderWidth: 10, borderColor: '#317AF7', @@ -319,7 +321,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 0, borderWidth: 10, borderColor: '#317AF7', @@ -336,7 +338,7 @@ struct Index { tileMode: TileMode.REPEAT, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 0, borderWidth: 10, borderColor: '#317AF7', @@ -353,7 +355,7 @@ struct Index { tileMode: TileMode.CLAMP, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 0, borderWidth: 10, borderColor: '#317AF7', @@ -370,7 +372,7 @@ struct Index { tileMode: TileMode.MIRROR, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 0, borderWidth: 10, borderColor: '#317AF7', @@ -379,7 +381,7 @@ struct Index { ] private colorItems: PictureItem [] = [ { - src: null, + src: '', srcType: null, isSvg: false, primaryTitle: 'Color', @@ -396,7 +398,7 @@ struct Index { padding: 0 }, { - src: null, + src: '', srcType: null, isSvg: false, primaryTitle: 'Color', @@ -413,7 +415,7 @@ struct Index { padding: 0 }, { - src: null, + src: '', srcType: null, isSvg: false, primaryTitle: 'Color', @@ -430,7 +432,7 @@ struct Index { padding: 0 }, { - src: null, + src: '', srcType: null, isSvg: false, primaryTitle: 'Color', @@ -447,7 +449,7 @@ struct Index { padding: 0 }, { - src: null, + src: '', srcType: null, isSvg: false, primaryTitle: 'Color', @@ -464,7 +466,7 @@ struct Index { padding: 0 }, { - src: null, + src: '', srcType: null, isSvg: false, primaryTitle: 'Color', @@ -481,7 +483,7 @@ struct Index { padding: 0 }, { - src: null, + src: '', srcType: null, isSvg: false, primaryTitle: 'Color', @@ -682,7 +684,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -699,7 +701,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -716,7 +718,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -733,7 +735,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -750,7 +752,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -767,7 +769,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -784,7 +786,7 @@ struct Index { tileMode: null, uiWidth: this.uiWidth, uiHeight: this.uiHeight, - backgroundColor: null, + backgroundColor: '', cornerRadius: 25, borderWidth: 10, borderColor: '#317AF7', @@ -801,8 +803,8 @@ struct Index { private initViewModels(): void { let viewModelsLength = Math.max(this.picIdxArr.length, this.colorIdxArr.length) - for (var index = 0; index < viewModelsLength; index++) { - this.viewModels.push(new RoundedImageView.Model) + for (let index = 0; index < viewModelsLength; index++) { + this.viewModels.push(new RoundedImageName.Model) } } @@ -844,8 +846,8 @@ struct Index { } private initUri() { - FileUtils.getInstance().createFolder(this.uriFolder) - globalThis.resourceManager.getMedia($r('app.media.photo1').id, (error, value) => { + FileUtils.getInstance().createFolder(this.uriFolder); + (GlobalContext.getContext().getObject("context") as Context).resourceManager.getMedia($r('app.media.photo1').id, (error: Error, value: Uint8Array) => { FileUtils.getInstance().writePic(this.uriFile, value.buffer) }) } @@ -863,35 +865,35 @@ struct Index { Scroll(this.scroller) { List({ space: 10, initialIndex: 0 }) { if (this.typeValue == 'Bitmap') { - ForEach(this.picIdxArr, (item) => { + ForEach(this.picIdxArr, (item:number) => { ListItem() { this.ViewItem(this.viewModels[item], this.rectPictureItems[item]) }.editable(false) - }, item => item) + }, (item:string) => item) } else if (this.typeValue == 'Ovals') { - ForEach(this.picIdxArr, (item) => { + ForEach(this.picIdxArr, (item:number) => { ListItem() { this.ViewItem(this.viewModels[item], this.ovalPictureItems[item]) }.editable(false) - }, item => item) + }, (item:string) => item) } else if (this.typeValue == 'Color') { - ForEach(this.colorIdxArr, (item) => { + ForEach(this.colorIdxArr, (item:number) => { ListItem() { this.ViewItem(this.viewModels[item], this.colorItems[item]) }.editable(false) - }, item => item) + }, (item:string) => item) } else if (this.typeValue == 'Background') { - ForEach(this.picIdxArr, (item) => { + ForEach(this.picIdxArr, (item:number) => { ListItem() { this.ViewItem(this.viewModels[item], this.backgroundColorItems[item]) }.editable(false) - }, item => item) + }, (item:string) => item) } else if (this.typeValue == 'SVG') { - ForEach(this.colorIdxArr, (item) => { + ForEach(this.colorIdxArr, (item:number) => { ListItem() { this.ViewItem(this.viewModels[item], this.svgItems[item]) }.editable(false) - }, item => item) + }, (item:string) => item) } } } @@ -904,7 +906,7 @@ struct Index { } } - @Builder ViewItem(roundedImageViewModel: RoundedImageView.Model, pictureItem: PictureItem) { + @Builder ViewItem(roundedImageViewModel: RoundedImageName.Model, pictureItem: PictureItem) { Column({ space: 5 }) { RoundedImageView({ model: roundedImageViewModel }) Column() { diff --git a/entry/src/main/ets/pages/PictureItem.ets b/entry/src/main/ets/pages/PictureItem.ets index c2bf923..529c077 100644 --- a/entry/src/main/ets/pages/PictureItem.ets +++ b/entry/src/main/ets/pages/PictureItem.ets @@ -18,13 +18,13 @@ import { ScaleType, TileMode, SrcType } from '@ohos/roundedimageview/' export interface PictureItem { src: string | Resource | ArrayBuffer - srcType: SrcType + srcType: SrcType | null isSvg: boolean primaryTitle: string secondTitle: string scaleTypeName: string scaleType: ScaleType - tileMode: TileMode + tileMode: TileMode | null uiWidth: number uiHeight: number backgroundColor: string | CanvasGradient | CanvasPattern diff --git a/entry/src/main/ets/pages/TypeCustomDialog.ets b/entry/src/main/ets/pages/TypeCustomDialog.ets index 3b2fd3b..ae9cd9c 100644 --- a/entry/src/main/ets/pages/TypeCustomDialog.ets +++ b/entry/src/main/ets/pages/TypeCustomDialog.ets @@ -15,7 +15,7 @@ @CustomDialog export struct TypeCustomDialog { - private controller: CustomDialogController + private controller?: CustomDialogController private typeArr: string[] = ['Bitmap', 'Ovals', 'Color', 'Background', 'SVG'] @Link private typeValue: string @@ -23,7 +23,7 @@ export struct TypeCustomDialog { Row() { Scroll() { List({ space: 10, initialIndex: 0 }) { - ForEach(this.typeArr, (item) => { + ForEach(this.typeArr, (item:string) => { ListItem() { Text(item) .width('100%') @@ -33,10 +33,10 @@ export struct TypeCustomDialog { .backgroundColor(0xFFFFFF) .onClick(() => { this.typeValue = item - this.controller.close(); + this.controller?.close(); }) } - }, item => item) + }, (item:string) => item) }.backgroundColor(Color.White).width(200).padding(20) } } -- Gitee From a0c31ff561cd8b2cc5d3261bb9370c6a26ab137b Mon Sep 17 00:00:00 2001 From: xinhu <1216197016@qq.com> Date: Fri, 15 Sep 2023 15:57:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?ArkTS=E8=AF=AD=E6=B3=95=E8=A7=84=E5=88=99?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xinhu <1216197016@qq.com> --- AppScope/app.json5 | 2 +- RoundedImageView/index.ets | 2 ++ RoundedImageView/oh-package.json5 | 4 ++-- .../src/main/ets/components}/GlobalContext.ts | 17 ++++++++++++++++- .../src/main/ets/components/PixelMapUtils.ts | 3 +-- .../main/ets/components/RoundedImageView.ets | 3 ++- build-profile.json5 | 8 ++++++-- entry/oh-package.json5 | 4 ++-- entry/src/main/ets/entryability/EntryAbility.ts | 9 ++------- entry/src/main/ets/pages/Index.ets | 3 +-- entry/src/ohosTest/ets/test/Ability.test.ets | 12 ++++++------ .../ohosTest/ets/testability/TestAbility.ets | 11 ++++------- hvigor/hvigor-config.json5 | 6 +++--- oh-package.json5 | 4 ++-- 14 files changed, 50 insertions(+), 38 deletions(-) rename {entry/src/main/ets/entryability => RoundedImageView/src/main/ets/components}/GlobalContext.ts (43%) diff --git a/AppScope/app.json5 b/AppScope/app.json5 index f4f2da0..782c962 100644 --- a/AppScope/app.json5 +++ b/AppScope/app.json5 @@ -3,7 +3,7 @@ "bundleName": "com.example.roundimageview", "vendor": "example", "versionCode": 1000003, - "versionName": "1.0.4", + "versionName": "2.0.1", "icon": "$media:app_icon", "label": "$string:app_name", "distributedNotificationEnabled": true diff --git a/RoundedImageView/index.ets b/RoundedImageView/index.ets index 7282bdb..300ad87 100644 --- a/RoundedImageView/index.ets +++ b/RoundedImageView/index.ets @@ -22,3 +22,5 @@ export { default as TileMode } from './src/main/ets/components/TileMode' export { default as SrcType } from './src/main/ets/components/SrcType' export { default as FileUtils } from './src/main/ets/components/FileUtils' + +export { GlobalContext } from './src/main/ets/components/GlobalContext' diff --git a/RoundedImageView/oh-package.json5 b/RoundedImageView/oh-package.json5 index 5ebfbbc..205e21a 100644 --- a/RoundedImageView/oh-package.json5 +++ b/RoundedImageView/oh-package.json5 @@ -12,8 +12,8 @@ "description": "RoundedImageView支持许多附加功能,包括椭圆、圆角矩形、ScaleTypes 和 TileModes", "main": "index", "repository": "https://gitee.com/openharmony-sig/RoundedImageView", - "version": "2.0.0", + "version": "2.0.1", "dependencies": { "@ohos/svg": "2.0.0" } -} \ No newline at end of file +} diff --git a/entry/src/main/ets/entryability/GlobalContext.ts b/RoundedImageView/src/main/ets/components/GlobalContext.ts similarity index 43% rename from entry/src/main/ets/entryability/GlobalContext.ts rename to RoundedImageView/src/main/ets/components/GlobalContext.ts index 64b5f29..0595a2e 100644 --- a/entry/src/main/ets/entryability/GlobalContext.ts +++ b/RoundedImageView/src/main/ets/components/GlobalContext.ts @@ -1,5 +1,20 @@ +// @ts-nocheck +/* + * Copyright (C) 2022 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. + */ // 构造单例对象 -export class GlobalContext { +export class GlobalContext { private constructor() {} private static instance: GlobalContext; private _objects = new Map(); diff --git a/RoundedImageView/src/main/ets/components/PixelMapUtils.ts b/RoundedImageView/src/main/ets/components/PixelMapUtils.ts index 6bd31aa..5116b60 100644 --- a/RoundedImageView/src/main/ets/components/PixelMapUtils.ts +++ b/RoundedImageView/src/main/ets/components/PixelMapUtils.ts @@ -17,8 +17,7 @@ import image from '@ohos.multimedia.image'; import SrcType from './srctype'; import DownloadUtils from './downloadutils'; -import { GlobalContext } from '../../../../../entry/src/main/ets/entryability/GlobalContext' -import common from '@ohos.app.ability.common'; +import { GlobalContext } from './GlobalContext'; class PixelMapUtils { public static createPixelMap(src: string | Resource | ArrayBuffer, srcType: SrcType | null, callback: (pixelMap: image.PixelMap, width: number, height: number, error?: Error) => void): void { diff --git a/RoundedImageView/src/main/ets/components/RoundedImageView.ets b/RoundedImageView/src/main/ets/components/RoundedImageView.ets index 16d274e..ead81f8 100644 --- a/RoundedImageView/src/main/ets/components/RoundedImageView.ets +++ b/RoundedImageView/src/main/ets/components/RoundedImageView.ets @@ -18,7 +18,8 @@ import PixelMapUtils from './pixelmaputils'; import ScaleType from './scaletype'; import TileMode from './tilemode'; import SrcType from './srctype'; -import { GlobalContext } from '../../../../../entry/src/main/ets/entryability/GlobalContext' +import { GlobalContext } from './GlobalContext'; + @Component export struct RoundedImageView { diff --git a/build-profile.json5 b/build-profile.json5 index 696f607..2eb6fd3 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -1,12 +1,16 @@ { "app": { - "compileSdkVersion": 9, - "compatibleSdkVersion": 9, "products": [ { "name": "default", "signingConfig": "default", + "compileSdkVersion": 10, + "compatibleSdkVersion": 10, + "runtimeOS": "OpenHarmony" } + ], + "signingConfigs": [ + ] }, "modules": [ diff --git a/entry/oh-package.json5 b/entry/oh-package.json5 index 13fe9c2..2fc2923 100644 --- a/entry/oh-package.json5 +++ b/entry/oh-package.json5 @@ -4,8 +4,8 @@ "name": "entry", "description": "example description", "repository": {}, - "version": "2.0.0", + "version": "2.0.1", "dependencies": { "@ohos/roundedimageview": "file:../Roundedimageview" } -} \ No newline at end of file +} diff --git a/entry/src/main/ets/entryability/EntryAbility.ts b/entry/src/main/ets/entryability/EntryAbility.ts index b7b8a40..b97fc96 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ts +++ b/entry/src/main/ets/entryability/EntryAbility.ts @@ -17,22 +17,17 @@ import UIAbility from '@ohos.app.ability.UIAbility'; import hilog from '@ohos.hilog'; import window from '@ohos.window'; import display from '@ohos.display'; -import { GlobalContext } from './GlobalContext' +import { GlobalContext } from '@ohos/roundedimageview/src/main/ets/components/GlobalContext'; + export default class EntryAbility extends UIAbility { onCreate(want, launchParam) { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); - // globalThis.abilityWant = want; let displayWH = display.getDefaultDisplaySync() GlobalContext.getContext().setObject("abilityWant", want); GlobalContext.getContext().setObject("globalFilesDir", this.context.filesDir); GlobalContext.getContext().setObject("deviceW", displayWH.width); GlobalContext.getContext().setObject("deviceH", displayWH.height); GlobalContext.getContext().setObject("context", this.context); - // globalThis.globalFilesDir = this.context.filesDir - // let displayWH = display.getDefaultDisplaySync() - // globalThis.deviceW = displayWH.width; - // globalThis.deviceH = displayWH.height; - // globalThis.context = this.context } onDestroy() { diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 46e2890..67f6a25 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -15,8 +15,7 @@ import { PictureItem } from './PictureItem' import { TypeCustomDialog } from './TypeCustomDialog' -import { RoundedImageView,RoundedImageName, ScaleType, TileMode, SrcType, FileUtils } from '@ohos/roundedimageview' -import { GlobalContext } from '../../../../../entry/src/main/ets/entryability/GlobalContext' +import { RoundedImageView,RoundedImageName, ScaleType, TileMode, SrcType, FileUtils, GlobalContext } from '@ohos/roundedimageview' @Entry @Component diff --git a/entry/src/ohosTest/ets/test/Ability.test.ets b/entry/src/ohosTest/ets/test/Ability.test.ets index c5767a5..a7a68e9 100644 --- a/entry/src/ohosTest/ets/test/Ability.test.ets +++ b/entry/src/ohosTest/ets/test/Ability.test.ets @@ -17,27 +17,27 @@ import hilog from '@ohos.hilog'; import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' export default function abilityTest() { - describe('ActsAbilityTest', function () { + describe('ActsAbilityTest', () => { // Defines a test suite. Two parameters are supported: test suite name and test suite function. - beforeAll(function () { + beforeAll( () => { // Presets an action, which is performed only once before all test cases of the test suite start. // This API supports only one parameter: preset action function. }) - beforeEach(function () { + beforeEach( () => { // Presets an action, which is performed before each unit test case starts. // The number of execution times is the same as the number of test cases defined by **it**. // This API supports only one parameter: preset action function. }) - afterEach(function () { + afterEach( () => { // Presets a clear action, which is performed after each unit test case ends. // The number of execution times is the same as the number of test cases defined by **it**. // This API supports only one parameter: clear action function. }) - afterAll(function () { + afterAll( () => { // Presets a clear action, which is performed after all test cases of the test suite end. // This API supports only one parameter: clear action function. }) - it('assertContain',0, function () { + it('assertContain',0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); let a = 'abc' diff --git a/entry/src/ohosTest/ets/testability/TestAbility.ets b/entry/src/ohosTest/ets/testability/TestAbility.ets index 63b809e..d3c905d 100644 --- a/entry/src/ohosTest/ets/testability/TestAbility.ets +++ b/entry/src/ohosTest/ets/testability/TestAbility.ets @@ -19,18 +19,15 @@ import hilog from '@ohos.hilog'; import { Hypium } from '@ohos/hypium'; import testsuite from '../test/List.test'; import window from '@ohos.window'; - +import Want from '@ohos.app.ability.Want'; +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; export default class TestAbility extends UIAbility { - onCreate(want, launchParam) { + onCreate(want:Want, launchParam:AbilityConstant.LaunchParam) { hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onCreate'); hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? ''); hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:'+ JSON.stringify(launchParam) ?? ''); - var abilityDelegator: any - abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() - var abilityDelegatorArguments: any - abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() hilog.info(0x0000, 'testTag', '%{public}s', 'start run testcase!!!'); - Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite) + Hypium.hypiumTest(AbilityDelegatorRegistry.getAbilityDelegator(), AbilityDelegatorRegistry.getArguments(), testsuite) } onDestroy() { diff --git a/hvigor/hvigor-config.json5 b/hvigor/hvigor-config.json5 index ff68812..e75281b 100644 --- a/hvigor/hvigor-config.json5 +++ b/hvigor/hvigor-config.json5 @@ -1,6 +1,6 @@ { - "hvigorVersion": "2.0.0", + "hvigorVersion": "3.0.2", "dependencies": { - "@ohos/hvigor-ohos-plugin": "2.0.0" + "@ohos/hvigor-ohos-plugin": "3.0.2" } -} +} \ No newline at end of file diff --git a/oh-package.json5 b/oh-package.json5 index 5797065..aed62c9 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -6,6 +6,6 @@ "name": "roundimageview", "description": "example description", "repository": {}, - "version": "2.0.0", + "version": "2.0.1", "dependencies": {} -} \ No newline at end of file +} -- Gitee