HarmonyOS鸿蒙Next中实现tabbar选中凸起效果

HarmonyOS鸿蒙Next中实现tabbar选中凸起效果 如何实现在tabbar被选中后,凸起的效果

4 回复

@Entry @Component struct onePage { @State fontColor: string = ‘#182431’ @State selectedFontColor: string = ‘#007DFF’ @State currentIndex: number = 0 private controller: TabsController = new TabsController() tabTitle: string[] = [‘健康’, ‘商城’, ‘预警中心’, ‘消息’, ‘我的’]

@Builder imgText(item: string, index: number) { if(this.currentIndex == index){ Column() { Column() { Image($r(‘app.media.startIcon’)) .height(50) .objectFit(ImageFit.Contain) .borderRadius(100) } .height(60) .width(60) .offset({ y: -20 }) .backgroundColor(Color.White) .borderRadius(100) } .width(‘20%’) .height(‘100%’) .backgroundColor(Color.Blue) .justifyContent(FlexAlign.Center) }else{ Column({ space: 10 }) { Image($r(‘app.media.startIcon’)) .height(40) .objectFit(ImageFit.Contain) Text(item) .fontSize(15) } .width(‘20%’) .height(‘100%’) .backgroundColor(Color.Blue) .onClick(() => { this.currentIndex = index this.controller.changeIndex(this.currentIndex) }) } }

@Builder bottomBuilder() { Row() { ForEach(this.tabTitle, (item: string, index: number) => { this.imgText(item, index) }) } .justifyContent(FlexAlign.SpaceEvenly) .width(‘100%’) .height(‘10%’) }

build() { Column() { Tabs({ index: this.currentIndex, controller: this.controller, barPosition: BarPosition.End }) { TabContent() { Column().width(‘100%’).height(‘100%’).backgroundColor(’#00CB87’) }

    TabContent() {
      Column().width('100%').height('100%').backgroundColor('#007DFF')
    }

    TabContent() {
      Column().width('100%').height('100%').backgroundColor('#FFBF00')
    }

    TabContent() {
      Column().width('100%').height('100%').backgroundColor('#cccccc')
    }
  }
  .onChange((index: number) => {
    this.currentIndex = index
  })
  .width('100%')
}
.overlay(this.bottomBuilder(), { align: Alignment.End, offset: { x: 0, y: 360 } })
.width('100%')
.height('100%')

} }

更多关于HarmonyOS鸿蒙Next中实现tabbar选中凸起效果的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


姓名:张三
职位:软件工程师
邮箱:zhangsan@example.com
电话:123-456-7890

简介:
具有五年软件开发经验,熟悉Java、Python和C++。参与多个大型项目,擅长系统设计和问题解决。

在HarmonyOS鸿蒙Next中实现tabbar选中凸起效果,可以通过自定义组件和动画效果来实现。首先,使用TabContent组件创建基本的tabbar布局。然后,通过Shape组件定义凸起的形状,例如半圆形或矩形。使用State管理选中状态,并在选中时应用凸起效果。

具体步骤如下:

  1. 创建TabContent组件,设置TabItem为每个tab的内容。
  2. 使用Shape组件定义凸起形状,设置Path属性绘制所需形状。
  3. TabContentonChange事件中,根据选中的TabItem更新State
  4. 使用Animation组件为凸起效果添加动画,例如SpringAnimationKeyframeAnimation
  5. State变化时,应用动画效果到Shape组件,实现凸起效果。

示例代码:

@Entry
@Component
struct TabBarWithBump {
  @State selectedIndex: number = 0;

  build() {
    Column() {
      TabContent({ barPosition: BarPosition.BOTTOM }) {
        TabItem('Tab1').content(() => {
          Text('Content 1')
        })
        TabItem('Tab2').content(() => {
          Text('Content 2')
        })
      }
      .onChange((index: number) => {
        this.selectedIndex = index;
      })

      Shape() {
        Path()
          .commands('M 0 0 L 100 0 L 100 50 Q 50 70 0 50 Z')
          .fill(Color.Blue)
          .animation({
            duration: 300,
            curve: Curve.EaseInOut
          })
      }
      .height(50)
      .width(100)
      .offset({ y: this.selectedIndex === 0 ? -10 : 0 })
    }
  }
}

该代码展示了如何在鸿蒙Next中实现tabbar选中时的凸起效果。通过Shape组件和Animation组件,可以实现平滑的凸起动画。

在HarmonyOS鸿蒙Next中实现TabBar选中凸起效果,可以通过自定义TabBar组件并结合ShapeAnimation来实现。首先,使用Shape组件创建一个凸起的背景形状,然后在选中Tab时,通过Animation动态调整形状的高度和位置。具体步骤包括:定义TabBar布局、创建凸起形状、设置选中状态样式以及添加动画效果。这样,当用户点击不同Tab时,凸起效果会平滑过渡到选中的Tab项,提升用户体验。

回到顶部