HarmonyOS 组件放大缩小demo

希望提供定制化的组件demo,包含一个组件放大缩小的动画效果,以及支持传入子组件(比如 TextInput,TextPicker等)

HarmonyOS
2024-12-24 17:55:25
663浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

示例如下:

@Entry
@Component
struct TextInputTest {
  @State message: string = 'Hello World';
  private controller:TextInputController = new TextInputController();
  //输入框初始化状态
  @State marginLeft1:number = 40
  @State marginBottom1:number = 150
  @State fontSize1:number = 20
  @State textAreaValue:string = ""

  @State marginLeft2:number = 40
  @State marginBottom2:number = 0
  @State fontSize2:number = 20
  @State textInputValue:string = ""
  //文本选择器弹窗状态
  private selected: number | number[] = 0
  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
  @State selectValue:string = '';
  @State marginLeft3:number = 40
  @State marginBottom3:number = 0
  @State fontSize3:number = 20

  @State modifier1: TextModifier = new TextModifier(this.marginLeft1,this.marginBottom1,this.fontSize1)
  @State modifier2: TextModifier = new TextModifier(this.marginLeft2,this.marginBottom2,this.fontSize2)
  @State modifier3: TextModifier = new TextModifier(this.marginLeft3,this.marginBottom3,this.fontSize3)

  build() {
    Column(){
      //TextArea多行输入框
      Column(){
        Stack() {
          Text("TextArea多行输入框")
            .fontSize(this.modifier1.fontSize)
            .textAlign(TextAlign.Start)
            .margin({left:this.modifier1.marginLeft,bottom:this.modifier1.marginBottom})
            .attributeModifier(this.modifier1)
            .width("100%")
            .animation({
              duration: 300,
              curve: Curve.Friction,
              iterations: 1,
              playMode: PlayMode.Normal
            })

          TextArea({text:$$this.textAreaValue})
            .onFocus(() => {
              if(this.textAreaValue === ""){
                this.modifier1.isFocus = true
              }
            })
            .onBlur(()=>{
              //如果未输入任何内容,提示性标题复原
              if(this.textAreaValue === ""){
                this.modifier1.isFocus = false
              }
            })
            .height("80%")
          // .backgroundColor("#00FFFFFF")  //背景透明

        }
      }
      .justifyContent(FlexAlign.Center)
      .height("30%")

      //TextInput单行输入框
      Column(){
        Stack() {
          Text("TextInput单行输入框")
            .fontSize(this.modifier2.fontSize)
            .textAlign(TextAlign.Start)
            .width("100%")
            .margin({left:this.modifier2.marginLeft,bottom:this.modifier2.marginBottom})
            .attributeModifier(this.modifier2)
            .animation({
              duration: 300,
              curve: Curve.Friction,
              iterations: 1,
              playMode: PlayMode.Normal
            })


          TextInput({text:$$this.textInputValue})
            .onFocus(() => {
              if(this.textInputValue === ""){
                this.modifier2.isFocus = true
              }
            })
            .onBlur(()=>{
              //如果未输入任何内容,提示性标题复原
              if(this.textInputValue === ""){
                this.modifier2.isFocus = false
              }
            })
          // .backgroundColor("#00FFFFFF")  //背景透明
        }
      }
      .justifyContent(FlexAlign.Center)
      .height("30%")

      //文本选择弹窗
      Column(){
        Stack(){
          Text("testPickerTest")
            .width("100%")
            .fontSize(this.fontSize3)
            .margin({left:this.marginLeft3,bottom:this.marginBottom3})
            .animation({
              duration: 300,
              curve: Curve.Friction,
              iterations: 1,
              playMode: PlayMode.Normal
            })

          Row(){
            Text(this.selectValue)
              .width("80%")
              .margin({left:10})
            Image($r("app.media.startIcon"))
              .height(20)
              .margin({right:10})
              .onClick(() => {
                TextPickerDialog.show({
                  range: this.fruits,
                  selected: this.selected,
                  onAccept: (value: TextPickerResult) => {
                    //选择完成改变位置
                    this.marginLeft3 = 10
                    this.marginBottom3 = 50
                    this.fontSize3 = 13
                    // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
                    this.selected = value.index
                    console.log(this.selected + '')
                    // 点击确定后,被选到的文本数据展示到页面
                    this.selectValue = value.value as string
                    console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
                  },
                })
              })
          }
          .backgroundColor("#33eeb8b8")  //背景
          .width("100%")
          .justifyContent(FlexAlign.SpaceBetween)
        }
      }
      .backgroundColor("#33f1d8d8")  //背景
      .justifyContent(FlexAlign.Center)
      .height("30%")
      // .height("50%")

    }
    .height('100%')
    .width('100%')
  }

}

//全局动态属性
class TextModifier implements AttributeModifier<TextAttribute> {
  isFocus: boolean|undefined = undefined
  marginLeft:number = 0
  marginBottom:number = 0
  fontSize:number = 0
  applyNormalAttribute(instance: TextAttribute): void {
    if (this.isFocus) {
      this.marginLeft -= 10
      this.marginBottom += 50
      this.fontSize -= 7
      instance
        .fontSize(this.fontSize)
        .margin({left:this.marginLeft,bottom:this.marginBottom})
    } else if(this.isFocus === false) {
      //初始化时会执行该回调,所以 isFocus 初始值为undefined
      this.marginLeft += 10
      this.marginBottom -= 50
      this.fontSize += 7
      instance
        .fontSize(this.fontSize)
        .margin({left:this.marginLeft,bottom:this.marginBottom})
    }
  }

  constructor(marginLeft: number, marginBottom: number, fontSize: number) {
    this.marginLeft = marginLeft;
    this.marginBottom = marginBottom;
    this.fontSize = fontSize;
  }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
分享
微博
QQ
微信
回复
2024-12-24 19:57:03


相关问题
HarmonyOS 点击图片放大缩小
833浏览 • 1回复 待解决
HarmonyOS 相机预览是否支持放大缩小
673浏览 • 1回复 待解决
HarmonyOS 如何实现放大缩小的动画?
1177浏览 • 1回复 待解决
Swiper轮播图带放大缩小动效实践
2401浏览 • 1回复 待解决
Image组件如何实现双指手势放大
3166浏览 • 1回复 待解决
HarmonyOS 想点击图片放大
680浏览 • 1回复 待解决