From 8e8205a1b5f2f1464bb0eed9405ce1208cefe7cc Mon Sep 17 00:00:00 2001 From: zenggaofeng Date: Wed, 17 Jan 2024 16:19:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BD=BF=E7=94=A8RequestOpti?= =?UTF-8?q?on=E6=98=BE=E7=A4=BA=E5=9B=BE=E7=89=87=E6=A0=B7=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zenggaofeng --- .../main/ets/pages/RequestOptionLoadImage.ets | 104 ++++++++++++++++++ .../ets/pages/imageknifeTestCaseIndex.ets | 5 + .../resources/base/profile/main_pages.json | 3 +- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 entry/src/main/ets/pages/RequestOptionLoadImage.ets diff --git a/entry/src/main/ets/pages/RequestOptionLoadImage.ets b/entry/src/main/ets/pages/RequestOptionLoadImage.ets new file mode 100644 index 0000000..4b7fb99 --- /dev/null +++ b/entry/src/main/ets/pages/RequestOptionLoadImage.ets @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2024 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 image from '@ohos.multimedia.image' +import { + ImageKnifeData, + RequestOption, + Size, + ImageKnife, + ImageKnifeGlobal, + ImageKnifeComponent +} from '@ohos/libraryimageknife' +import { BusinessError } from '@ohos.base' + +const TAG = "TEST-" +let timeId = -1 + +@Entry +@Component +struct RequestOptionLoadImage { + @State pixelMap: PixelMap | undefined = undefined + + load(src: string | image.PixelMap | Resource) { + clearTimeout(timeId) + let request = new RequestOption() + request.load(src) + .addListener({ callback: (err: BusinessError | string, data: ImageKnifeData) => { + if (data.isPixelMap()) { + if (data.drawPixelMap) { + let pixelmap = data.drawPixelMap.imagePixelMap + if (pixelmap) { + this.pixelMap = pixelmap + } + } + } + if (data.isGIFFrame()) { + let index: number = 0 + if (data.drawGIFFrame) { + if (data.drawGIFFrame.imageGIFFrames) { + let renderGif = () => { + if (data.drawGIFFrame) { + if (data.drawGIFFrame.imageGIFFrames) { + let pixelmap = data.drawGIFFrame.imageGIFFrames[index].drawPixelMap + let delay = data.drawGIFFrame.imageGIFFrames[index].delay + if (pixelmap) { + this.pixelMap = pixelmap + } + index++; + if (index == data.drawGIFFrame.imageGIFFrames.length - 1) { + index = 0 + } + timeId = setTimeout(renderGif, data!.drawGIFFrame!.imageGIFFrames![index].delay) + } + } + } + renderGif() + } + } + } + if (err) { + console.log(TAG + "error:" + JSON.stringify(err)); + } + return false + } + }) + let compSize: Size = { + width: 300, + height: 300 + } + request.setImageViewSize(compSize) + let imageknife: ImageKnife | undefined = ImageKnifeGlobal.getInstance().getImageKnife() + if (imageknife != undefined) { + imageknife.call(request) + } + } + + build() { + Scroll() { + Column() { + Text("RequestOption加载图片").fontSize(25) + Button("加载网络gif").onClick(() => { + this.load("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658") + }) + Button("加载静态图").onClick(() => { + this.load($r('app.media.pngSample')) + }) + ImageKnifeComponent({ imageKnifeOption: { + loadSrc: this.pixelMap as image.PixelMap + } }).width(300).height(300).borderWidth(3) + } + } + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/imageknifeTestCaseIndex.ets b/entry/src/main/ets/pages/imageknifeTestCaseIndex.ets index 07db954..0161792 100644 --- a/entry/src/main/ets/pages/imageknifeTestCaseIndex.ets +++ b/entry/src/main/ets/pages/imageknifeTestCaseIndex.ets @@ -71,6 +71,11 @@ struct IndexFunctionDemo { console.log("测试ImageKnifeComponent thumbnail") router.pushUrl({ url: "pages/testImageKnifeOptionChangedPage2" }); }).margin({ top: 5, left: 3 }) + Button("测试RequestOption加载图片") + .onClick(() => { + console.log("测试RequestOption加载图片") + router.pushUrl({ url: "pages/RequestOptionLoadImage" }); + }).margin({ top: 5, left: 3 }) }.width('100%').height(60).backgroundColor(Color.Pink) diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index 8de37ea..a119187 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -37,6 +37,7 @@ "pages/testManyGifLoadWithPage", "pages/testImageAntiAliasingWithPage", "pages/testImageKnifeRouter1", - "pages/testImageKnifeRouter2" + "pages/testImageKnifeRouter2", + "pages/RequestOptionLoadImage" ] } \ No newline at end of file