diff --git a/entry/src/main/ets/pages/frescoImageTestCasePage.ets b/entry/src/main/ets/pages/frescoImageTestCasePage.ets index 4534bce..997704f 100644 --- a/entry/src/main/ets/pages/frescoImageTestCasePage.ets +++ b/entry/src/main/ets/pages/frescoImageTestCasePage.ets @@ -58,10 +58,10 @@ struct FrescoImageTestCasePage { build() { Scroll() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { -// ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption1 }) -// ImageKnifeComponent({ imageKnifeOption: $ImageKnifeOption }) -// ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption3 }) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption4 }) +// ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }) +// ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption }) +// ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption3 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption4 }) // Scroll() { // Text(this.progresshint).fontSize(15) // }.width(300).height(200) diff --git a/entry/src/main/ets/pages/frescoRetryTestCasePage.ets b/entry/src/main/ets/pages/frescoRetryTestCasePage.ets index 4624896..d8fe743 100644 --- a/entry/src/main/ets/pages/frescoRetryTestCasePage.ets +++ b/entry/src/main/ets/pages/frescoRetryTestCasePage.ets @@ -64,10 +64,10 @@ struct FrescoImageTestCasePage { build() { Scroll() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { -// ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption1 }) -// ImageKnifeComponent({ imageKnifeOption: $ImageKnifeOption }) -// ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption3 }) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption4 }) +// ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }) +// ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption }) +// ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption3 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption4 }) } } .width('100%') diff --git a/entry/src/main/ets/pages/index.ets b/entry/src/main/ets/pages/index.ets index 7b4d7cc..3b7f00e 100644 --- a/entry/src/main/ets/pages/index.ets +++ b/entry/src/main/ets/pages/index.ets @@ -52,7 +52,7 @@ struct IndexFunctionDemo { errorholderSrc: $r('app.media.icon_failed') } }).margin({ top: 5, left: 3 }) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption1 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }) }.width('100%').backgroundColor(Color.Pink) Text("简单示例2:加载一张网络gif图片").fontSize(15) @@ -68,7 +68,7 @@ struct IndexFunctionDemo { displayProgress:true, } }).margin({ top: 5, left: 3 }) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption2 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption2 }) }.width('100%').backgroundColor(Color.Pink) Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { diff --git a/entry/src/main/ets/pages/showErrorholderTestCasePage.ets b/entry/src/main/ets/pages/showErrorholderTestCasePage.ets index c25b479..42f504e 100644 --- a/entry/src/main/ets/pages/showErrorholderTestCasePage.ets +++ b/entry/src/main/ets/pages/showErrorholderTestCasePage.ets @@ -52,10 +52,10 @@ struct ShowErrorholderTestCasePage { build() { Scroll() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption1 }) - ImageKnifeComponent({ imageKnifeOption: $ImageKnifeOption }) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption3 }) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption4 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption3 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption4 }) } } .width('100%') diff --git a/entry/src/main/ets/pages/testAllCacheInfoPage.ets b/entry/src/main/ets/pages/testAllCacheInfoPage.ets index dab58d5..66b5db1 100644 --- a/entry/src/main/ets/pages/testAllCacheInfoPage.ets +++ b/entry/src/main/ets/pages/testAllCacheInfoPage.ets @@ -126,7 +126,7 @@ struct TestAllCacheInfoPage { Scroll() { Text(this.cacheinfo3).fontSize(15) }.width(300).height(200) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption1 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }) Button("ImageKnifeComponent加载网络资源获取缓存信息").width(300).height(25) .onClick(() => { @@ -148,7 +148,7 @@ struct TestAllCacheInfoPage { Scroll() { Text(this.cacheinfo4).fontSize(15) }.width(300).height(200) - ImageKnifeComponent({ imageKnifeOption: $ImageKnifeOption }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption }) } } diff --git a/entry/src/main/ets/pages/testGifDontAnimatePage.ets b/entry/src/main/ets/pages/testGifDontAnimatePage.ets index f256bb1..b8d3107 100644 --- a/entry/src/main/ets/pages/testGifDontAnimatePage.ets +++ b/entry/src/main/ets/pages/testGifDontAnimatePage.ets @@ -83,7 +83,7 @@ struct TestGifDontAnimatePage { }).margin({left:15}).backgroundColor(Color.Grey) } .margin({top:15}) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption1 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }) } } diff --git a/entry/src/main/ets/pages/testImageKnifeOptionChangedPage.ets b/entry/src/main/ets/pages/testImageKnifeOptionChangedPage.ets index adeea94..47e964a 100644 --- a/entry/src/main/ets/pages/testImageKnifeOptionChangedPage.ets +++ b/entry/src/main/ets/pages/testImageKnifeOptionChangedPage.ets @@ -181,7 +181,7 @@ struct TestImageKnifeOptionChangedPage { Text('下面为展示图片区域').margin({top:5}) Flex({direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }){ - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption1 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }) }.width(400).height(400).margin({top:10}).backgroundColor(Color.Pink) diff --git a/entry/src/main/ets/pages/testImageKnifeOptionChangedPage2.ets b/entry/src/main/ets/pages/testImageKnifeOptionChangedPage2.ets index 024313f..61aab30 100644 --- a/entry/src/main/ets/pages/testImageKnifeOptionChangedPage2.ets +++ b/entry/src/main/ets/pages/testImageKnifeOptionChangedPage2.ets @@ -89,7 +89,7 @@ struct TestImageKnifeOptionChangedPage2 { Text("下面为展示图片区域").margin({top:5}) Flex({direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }){ - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption1 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }) }.width(400).height(400).margin({top:10}).backgroundColor(Color.Pink) diff --git a/entry/src/main/ets/pages/testImageKnifeOptionChangedPage3.ets b/entry/src/main/ets/pages/testImageKnifeOptionChangedPage3.ets index 812d872..17c7e23 100644 --- a/entry/src/main/ets/pages/testImageKnifeOptionChangedPage3.ets +++ b/entry/src/main/ets/pages/testImageKnifeOptionChangedPage3.ets @@ -230,7 +230,7 @@ struct TestImageKnifeOptionChangedPage3 { Text("下面为展示图片区域").margin({top:5}) Flex({direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }){ - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption1 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }) }.width(400).height(400).margin({top:10}).backgroundColor(Color.Pink) diff --git a/entry/src/main/ets/pages/testImageKnifeOptionChangedPage4.ets b/entry/src/main/ets/pages/testImageKnifeOptionChangedPage4.ets index 763c1e5..87180d2 100644 --- a/entry/src/main/ets/pages/testImageKnifeOptionChangedPage4.ets +++ b/entry/src/main/ets/pages/testImageKnifeOptionChangedPage4.ets @@ -102,7 +102,7 @@ struct TestImageKnifeOptionChangedPage4 { Text("下面为展示图片区域").margin({ top: 5 }) Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption1 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }) }.width(400).height(400).margin({ top: 10 }).backgroundColor(Color.Pink) } diff --git a/entry/src/main/ets/pages/testImageKnifeOptionChangedPage5.ets b/entry/src/main/ets/pages/testImageKnifeOptionChangedPage5.ets index be8abc0..1739548 100644 --- a/entry/src/main/ets/pages/testImageKnifeOptionChangedPage5.ets +++ b/entry/src/main/ets/pages/testImageKnifeOptionChangedPage5.ets @@ -89,7 +89,7 @@ struct TestImageKnifeOptionChangedPage5 { Text("下面为展示图片区域").margin({ top: 5 }) Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption1 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }) }.width(400).height(400).margin({ top: 10 }).backgroundColor(Color.Pink) } diff --git a/entry/src/main/ets/pages/testPreloadPage.ets b/entry/src/main/ets/pages/testPreloadPage.ets index 1bd9e42..1da8e2e 100644 --- a/entry/src/main/ets/pages/testPreloadPage.ets +++ b/entry/src/main/ets/pages/testPreloadPage.ets @@ -213,7 +213,7 @@ struct TestPreloadPage { } .margin({ top: 15 }) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption1 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }) } @@ -293,7 +293,7 @@ struct TestPreloadPage { } .margin({ top: 15 }) - ImageKnifeComponent({ imageKnifeOption: $ImageKnifeOption }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption }) } @@ -371,7 +371,7 @@ struct TestPreloadPage { } .margin({ top: 15 }) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption3 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption3 }) } @@ -449,7 +449,7 @@ struct TestPreloadPage { } .margin({ top: 15 }) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption4 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption4 }) } @@ -527,7 +527,7 @@ struct TestPreloadPage { } .margin({ top: 15 }) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption5 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption5 }) } @@ -605,7 +605,7 @@ struct TestPreloadPage { } .margin({ top: 15 }) - ImageKnifeComponent({ imageKnifeOption: $imageKnifeOption6 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption6 }) } } diff --git a/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets b/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets index 40b183b..108718d 100644 --- a/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets +++ b/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets @@ -23,22 +23,18 @@ import { IDrawLifeCycle } from '../imageknife/interface/IDrawLifeCycle' @Component export struct ImageKnifeComponent { - @Watch('watchImageKnifeOption') @Link imageKnifeOption: ImageKnifeOption; - @State componentWidth: string = '100%' - @State componentHeight: string = '100%' - drawLifeCycle: IDrawLifeCycle + @Watch('watchImageKnifeOption') @ObjectLink imageKnifeOption: ImageKnifeOption; - @State gifAutoPlay?:boolean = true; autoPlay:boolean = true - // 有效onAreaChanged触发计数 - private onAreaCount: number = 0 - private preSize: { - width: string, - height: string - } = { width: '0', height: '0' } + private settings: RenderingContextSettings = new RenderingContextSettings(true) private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) private hasDisplayRetryholder = false; + + + private lastWidth:number = 0 + private lastHeight:number = 0 + private currentWidth: number = 0 private currentHeight: number = 0 @@ -85,24 +81,28 @@ export struct ImageKnifeComponent { } } - + private canvasHasReady:boolean = false; build() { Canvas(this.context) - .width(this.componentWidth) - .height(this.componentHeight) + .width('100%') + .height('100%') .onAreaChange((oldValue: Area, newValue: Area) => { this.currentWidth = newValue.width as number this.currentHeight = newValue.height as number - console.log('onAreaChange stack currentWidth =' + this.currentWidth + ' currentHeight=' + this.currentHeight) - if (this.onAreaCount > 0) { - this.onAreaCount--; - this.imageKnifeExecute() + if(this.currentWidth <=0 || this.currentHeight <=0 ){ + // 存在宽或者高为0,此次重回无意义,无需进行request请求 + }else{ + if(this.currentHeight != this.lastHeight || this.currentWidth != this.lastWidth){ + // 宽高发生了变化 + console.log('onAreaChange isValid Canvas currentWidth =' + this.currentWidth + ' currentHeight=' + this.currentHeight) + this.lastWidth = this.currentWidth + this.lastHeight = this.currentHeight + this.imageKnifeExecute() + } } }) - .backgroundColor(this.imageKnifeOption.backgroundColor ? this.imageKnifeOption.backgroundColor : Color.White) - .margin(this.imageKnifeOption.margin ? this.imageKnifeOption.margin : { left: 0, top: 0, right: 0, bottom: 0 }) .onReady(() => { - + this.canvasHasReady = true; }) .onClick((event:ClickEvent) => { // 需要将点击事件回传 @@ -117,41 +117,46 @@ export struct ImageKnifeComponent { watchImageKnifeOption() { console.log('watchImageKnifeOption is happened!') - - if (this.imageKnifeOption.sizeAnimate) { - animateTo(this.imageKnifeOption.sizeAnimate, () => { - this.resetGifData(); - this.whetherWaitSize(); - }) - } else { +// if (this.imageKnifeOption.sizeAnimate) { +// animateTo(this.imageKnifeOption.sizeAnimate, () => { +// this.resetGifData(); +// this.whetherWaitSize(); +// }) +// } else { this.resetGifData(); this.whetherWaitSize(); - } +// } } whetherWaitSize() { - this.componentWidth = this.imageKnifeOption.size.width - this.componentHeight = this.imageKnifeOption.size.height - if (this.newSizeEqualPreSize(this.imageKnifeOption.size)) { - console.log('whetherWaitSize 宽高不变 直接发送请求') + if(this.currentHeight <= 0 || this.currentWidth <= 0){ + // 宽或者高没有高度,需要等待canvas组件初始化完成 + }else{ + console.log('whetherWaitSize 宽高有效 直接发送请求') this.imageKnifeExecute() - } else { - this.onAreaCount++; - // waitSize changed - this.preSize = this.imageKnifeOption.size - console.log('whetherWaitSize 宽高改变 等待组件回调onAreaChange后发送请求') } +// this.componentWidth = this.imageKnifeOption.size.width +// this.componentHeight = this.imageKnifeOption.size.height +// if (this.newSizeEqualPreSize(this.imageKnifeOption.size)) { +// console.log('whetherWaitSize 宽高不变 直接发送请求') +// this.imageKnifeExecute() +// } else { +// this.onAreaCount++; +// // waitSize changed +// this.preSize = this.imageKnifeOption.size +// console.log('whetherWaitSize 宽高改变 等待组件回调onAreaChange后发送请求') +// } } - newSizeEqualPreSize(newSize: { - width: string, - height: string - }): boolean { - if (this.preSize.width == newSize.width && this.preSize.height == newSize.height) { - return true; - } - return false; - } +// newSizeEqualPreSize(newSize: { +// width: string, +// height: string +// }): boolean { +// if (this.preSize.width == newSize.width && this.preSize.height == newSize.height) { +// return true; +// } +// return false; +// } retryClick() { this.hasDisplayRetryholder = false; @@ -160,9 +165,6 @@ export struct ImageKnifeComponent { aboutToAppear() { console.log('imageKnifeComponent aboutToAppear happened!') - this.onAreaCount++; - this.componentWidth = this.imageKnifeOption.size.width - this.componentHeight = this.imageKnifeOption.size.height } configNecessary(request: RequestOption) { @@ -173,13 +175,13 @@ export struct ImageKnifeComponent { return false; }) - if (this.imageKnifeOption.size) { +// if (this.imageKnifeOption.size) { let realSize = { width: this.currentWidth, height: this.currentHeight } request.setImageViewSize(realSize) - } +// } } configCacheStrategy(request: RequestOption) { @@ -261,7 +263,10 @@ export struct ImageKnifeComponent { } displayPlaceholder(data: ImageKnifeData) { - + if(!this.canvasHasReady){ + console.log('Canvas is not Initialized! displayPlaceholder is Failed!') + return; + } if (!this.drawLifeCycleHasConsumed(this.imageKnifeOption.drawLifeCycle, 'displayPlaceholder', this.context, data, this.imageKnifeOption, this.currentWidth, this.currentHeight, (gifTimeId) => { this.setGifTimeId(gifTimeId) @@ -280,7 +285,10 @@ export struct ImageKnifeComponent { } displayProgress(percent: number) { - + if(!this.canvasHasReady){ + console.log('Canvas is not Initialized! displayProgress is Failed!') + return; + } if (!this.drawLifeCycleHasConsumed(this.imageKnifeOption.drawLifeCycle, 'displayProgress', this.context, percent, this.imageKnifeOption, this.currentWidth, this.currentHeight, (gifTimeId) => { this.setGifTimeId(gifTimeId) @@ -299,7 +307,10 @@ export struct ImageKnifeComponent { } displayThumbSizeMultiplier(data: ImageKnifeData) { - + if(!this.canvasHasReady){ + console.log('Canvas is not Initialized! displayThumbSizeMultiplier is Failed!') + return; + } if (!this.drawLifeCycleHasConsumed(this.imageKnifeOption.drawLifeCycle, 'displayThumbSizeMultiplier', this.context, data, this.imageKnifeOption, this.currentWidth, this.currentHeight, (gifTimeId) => { this.setGifTimeId(gifTimeId) @@ -318,7 +329,10 @@ export struct ImageKnifeComponent { } displayMainSource(data: ImageKnifeData) { - + if(!this.canvasHasReady){ + console.log('Canvas is not Initialized! displayMainSource is Failed!') + return; + } if (!this.drawLifeCycleHasConsumed(this.imageKnifeOption.drawLifeCycle, 'displayMainSource', this.context, data, this.imageKnifeOption, this.currentWidth, this.currentHeight, (gifTimeId) => { this.setGifTimeId(gifTimeId) @@ -337,6 +351,11 @@ export struct ImageKnifeComponent { } displayRetryholder(data: ImageKnifeData) { + if(!this.canvasHasReady){ + console.log('Canvas is not Initialized! displayRetryholder is Failed!') + return; + } + if (!this.drawLifeCycleHasConsumed(this.imageKnifeOption.drawLifeCycle, 'displayRetryholder', this.context, data, this.imageKnifeOption, this.currentWidth, this.currentHeight, (gifTimeId) => { this.setGifTimeId(gifTimeId) @@ -354,7 +373,10 @@ export struct ImageKnifeComponent { } displayErrorholder(data: ImageKnifeData) { - + if(!this.canvasHasReady){ + console.log('Canvas is not Initialized! displayErrorholder is Failed!') + return; + } if (!this.drawLifeCycleHasConsumed(this.imageKnifeOption.drawLifeCycle, 'displayErrorholder', this.context, data, this.imageKnifeOption, this.currentWidth, this.currentHeight, (gifTimeId) => { this.setGifTimeId(gifTimeId) diff --git a/imageknife/src/main/ets/components/imageknife/ImageKnifeOption.ets b/imageknife/src/main/ets/components/imageknife/ImageKnifeOption.ets index 592fea0..e8be662 100644 --- a/imageknife/src/main/ets/components/imageknife/ImageKnifeOption.ets +++ b/imageknife/src/main/ets/components/imageknife/ImageKnifeOption.ets @@ -23,12 +23,8 @@ import { AllCacheInfo, IAllCacheInfoCallback } from '../imageknife/interface/IAl import { IDrawLifeCycle } from '../imageknife/interface/IDrawLifeCycle' import { ScaleType } from '../imageknife/ImageKnifeComponent' +@Observed export class ImageKnifeOption { - // 组件大小 - size: { - width: string, - height: string - }; // 主图资源 loadSrc: string | PixelMap | Resource; @@ -86,7 +82,7 @@ export class ImageKnifeOption { } - // 变换相关 + // 变换相关 不推荐使用该接口 建议直接使用transformation transformations这2个接口实现 transform?: { transformType: number, blur?: number, @@ -122,16 +118,9 @@ export class ImageKnifeOption { // 输出缓存相关内容和信息 allCacheInfoCallback?: IAllCacheInfoCallback; - imageFit?: ImageFit; - backgroundColor?: Color | number | string | Resource; - margin?: { - top?: number | string | Resource, - right?: number | string | Resource, - bottom?: number | string | Resource, - left?: number | string | Resource - } | number | string | Resource - sizeAnimate?: AnimateParam + +// sizeAnimate?: AnimateParam 由业务自定义不再支持 constructor() {