第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

HarmonyOS NEXT實(shí)戰(zhàn):網(wǎng)絡(luò)圖片加載和失敗占位圖

標(biāo)簽:
HarmonyOS

##HarmonyOS Next实战##HarmonyOS应用开发##教育##

目标:网络图片加载时,显示加载图,加载完毕后显示网络图片,加载失败则显示加载失败的占位图。

前提:需要申请权限ohos.permission.INTERNET。

实现思路:

  1. 通过Image显示图片。
  2. 通过Image的alt设置加载图。
  3. 通过Image的onError事件获取加载失败的状态。
  4. 根据加载状态显示对应的占位图。

接口说明

alt(value: string | Resource | PixelMap)

设置图片加载时显示的占位图。当组件的参数类型为AnimatedDrawableDescriptor时设置该属性不生效。
说明:加载时显示的占位图,支持本地图片(png、jpg、bmp、svg、gif和heif类型),支持PixelMap类型图片,不支持网络图片。(默认值:null)

onComplete事件

onComplete(callback: (event?: { width: number, height: number, componentWidth: number, componentHeight: number, loadingStatus: number,contentWidth: number, contentHeight: number, contentOffsetX: number, contentOffsetY: number }) => void)

图片数据加载成功和解码成功时均触发该回调,返回成功加载的图片尺寸。
当组件的参数类型为AnimatedDrawableDescriptor时该事件不触发。

onError事件

onError(callback: ImageErrorCallback)

图片加载异常时触发该回调。
当组件的参数类型为AnimatedDrawableDescriptor时该事件不触发。
说明:图片加载异常时触发的回调。建议开发者使用此回调,可快速确认图片加载失败时的具体原因。

ImageErrorCallback:
图片加载异常时触发的回调。
当组件的参数类型为AnimatedDrawableDescriptor时该事件不触发。

onFinish事件

onFinish(event: () => void)

当加载的源文件为带动效的svg格式图片时,svg动效播放完成时会触发这个回调。如果动效为无限循环动效,则不会触发这个回调。
仅支持svg格式的图片。当组件的参数类型为AnimatedDrawableDescriptor时该事件不触发。

aspectRatio接口

aspectRatio(value: number)

指定当前组件的宽高比,aspectRatio=width/height。
仅设置width、aspectRatio时,height=width/aspectRatio。
仅设置height、aspectRatio时,width=height*aspectRatio。
同时设置width、height和aspectRatio时,height不生效,height=width/aspectRatio。
设置aspectRatio属性后,组件宽高会受父组件内容区大小限制。

实战代码:

@Entry
@Component
struct ImagePlaceholderPage {
  imageSrc: string = 'https://c-ssl.dtstatic.com/uploads/blog/202311/27/0GSZv1oh0ePwpE.thumb.400_0.jpeg'
  @State loadingSuccess: boolean = true

  build() {
    Column({ space: 10 }) {
      Text('图片占位符')
      this.buildImage(this.imageSrc)
    }
    .width('100%')
    .height('100%')
    .padding(20)
  }

  @Builder
  buildImage(src: string) {
    Row() {
      if (this.loadingSuccess) {
        Image(src)
          .width('100%')// .alt($r('app.media.rays'))
          .onComplete(() => {
            //图片数据加载成功和解码成功时均触发该回调
            this.loadingSuccess = true
          })
          .onError(() => {
            //图片加载异常时触发该回调。
            this.loadingSuccess = false
          })
          .onFinish(() => {
            //当加载的源文件为带动效的svg格式图片时,svg动效播放完成时会触发这个回调。如果动效为无限循环动效,则不会触发这个回调。
          })
      } else {
        //图片加载失败的占位图
        Column({ space: 10 }) {
          SymbolGlyph($r('sys.symbol.exclamationmark_circle'))
            .fontSize(30)
            .renderingStrategy(SymbolRenderingStrategy.SINGLE)
            .fontColor([Color.Gray])
          Text('图片加载失败')
            .fontColor(Color.Gray)
        }
      }
    }
    .width('100%')
    .justifyContent(FlexAlign.Center)
    .borderRadius(6)
    .backgroundColor('#EEEEEE')
    .aspectRatio(1)
    .clip(true)
  }
}
點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消