chenjun3_wd

本地图片轮播

  1 +import { PhotoListBean } from 'wdBean/Index';
  2 +import { Logger } from 'wdKit/Index';
  3 +import { MultiPictureDetailItemComponent } from './MultiPictureDetailItemComponent';
  4 +import { display } from '@kit.ArkUI';
  5 +
  6 +const TAG = 'ImageSwiperComponent';
  7 +
  8 +@Component
  9 +export struct ImageSwiperComponent {
  10 + @Provide @Watch('onCurrentPageNumUpdated') currentPageNum: string = '01'
  11 + private scroller: Scroller = new Scroller()
  12 + @State swiperIndex: number = 0;
  13 + photoList: PhotoListBean[] = [];
  14 + private swiperController: SwiperController = new SwiperController()
  15 + private displayTool = display.getDefaultDisplaySync()
  16 + private screenWidth: number = 0
  17 + private picWidth: number = 0
  18 + @State picHeight: number = 0
  19 +
  20 + //watch监听页码回调
  21 + onCurrentPageNumUpdated(): void {
  22 + Logger.info(TAG, `currentPageNum:${this.currentPageNum}`,)
  23 + let _swiperIndex = Number.parseInt(this.currentPageNum)
  24 + Logger.info(TAG, `_swiperIndex:${_swiperIndex}`)
  25 + this.swiperIndex = _swiperIndex > 0 ? _swiperIndex - 1 : _swiperIndex
  26 + }
  27 +
  28 + aboutToAppear(): void {
  29 + //获取宽高尺寸
  30 + this.screenWidth = this.displayTool.width
  31 + // this.picWidth = this.screenWidth - vp2px(52)
  32 + this.picWidth = this.screenWidth
  33 + this.picHeight = this.picWidth * 578 / 375
  34 + }
  35 +
  36 + build() {
  37 + RelativeContainer() {
  38 + if (this.photoList && this.photoList?.length > 0) {
  39 + Swiper(this.swiperController) {
  40 + ForEach(this.photoList, (item: PhotoListBean) => {
  41 + MultiPictureDetailItemComponent({ MultiPictureDetailItem: item })
  42 + })
  43 + }
  44 + .index(this.swiperIndex)
  45 + .width('100%')
  46 + .height(px2vp(this.picHeight) + 32)
  47 + .vertical(false)
  48 + .autoPlay(false)
  49 + .cachedCount(3)
  50 + .indicator(false)
  51 + .displayCount(1)
  52 + .id('e_swiper_content')
  53 + .alignRules({
  54 + center: { anchor: "__container__", align: VerticalAlign.Center },
  55 + middle: { anchor: "__container__", align: HorizontalAlign.Center }
  56 + })
  57 + .onChange((index: number) => {
  58 + this.swiperIndex = index
  59 + })
  60 +
  61 + Row() {
  62 + Scroll(this.scroller) {
  63 + Row() {
  64 + Flex({
  65 + direction: FlexDirection.Column,
  66 + justifyContent: FlexAlign.Start
  67 + }) {
  68 + Text() {
  69 + Span(`${this.swiperIndex + 1}`)
  70 + .fontSize(24)
  71 + .fontFamily('PingFang SC-Medium')
  72 + .fontWeight(500)
  73 + .lineHeight(28)
  74 + Span(`/${this.photoList.length}`)
  75 + .fontSize(14)
  76 + .fontFamily('PingFang SC-Medium')
  77 + .fontWeight(500)
  78 + .lineHeight(19)
  79 + }.fontColor(Color.White).margin(4)
  80 + }
  81 + }
  82 + .width('100%')
  83 + .margin({
  84 + top: 8,
  85 + left: 18,
  86 + bottom: 24,
  87 + right: 18
  88 + })
  89 + }
  90 + .scrollable(ScrollDirection.Vertical)
  91 + .scrollBarWidth(0)
  92 + }
  93 + .id('e_swiper_titles')
  94 + .alignRules({
  95 + bottom: { anchor: "__container__", align: VerticalAlign.Bottom },
  96 + middle: { anchor: "__container__", align: HorizontalAlign.Center }
  97 + })
  98 + }
  99 + }
  100 + .width('100%')
  101 + .height('100%')
  102 + .backgroundColor(Color.Black)
  103 + .id('e_picture_container')
  104 + // 设置顶部绘制延伸到状态栏
  105 + // 设置底部绘制延伸到导航条
  106 + .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
  107 + }
  108 +}