新增使用RequestOption显示图片样例

Signed-off-by: zenggaofeng <zenggaofeng2@h-partners.com>
This commit is contained in:
zenggaofeng 2024-01-17 16:19:26 +08:00
parent 9204036789
commit 8e8205a1b5
3 changed files with 111 additions and 1 deletions

View File

@ -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)
}
}
}
}

View File

@ -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)

View File

@ -37,6 +37,7 @@
"pages/testManyGifLoadWithPage",
"pages/testImageAntiAliasingWithPage",
"pages/testImageKnifeRouter1",
"pages/testImageKnifeRouter2"
"pages/testImageKnifeRouter2",
"pages/RequestOptionLoadImage"
]
}