#鸿蒙通关秘籍#如何根据滑动动态地调整模糊效果?

HarmonyOS
2024-12-04 14:05:23
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
云端漫步者AJAX

可以通过监控触摸事件来动态调整模糊的程度。在下面的示例中,通过手指上下滑动来改变组件的模糊半径:

@Entry
@Component
struct Index1 {
  @State radius: number = 0;
  @State text: string = '';
  @State y: string = '手指不在屏幕上';

  aboutToAppear() {
    this.text = "按住屏幕上下滑动\n" + "当前手指所在y轴位置 : " + this.y +
    "\n" + "当前图片模糊程度为 : " + this.radius;
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
      Text(this.text)
        .height(200)
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
        .fontFamily("cursive")
        .fontStyle(FontStyle.Italic)
      Image($r("app.media.wall"))
        .blur(this.radius)
        .height('100%')
        .width("100%")
        .objectFit(ImageFit.Cover)
    }
    .height('100%')
    .width("100%")
    .onTouch((event?: TouchEvent) => {
      if(event) {
        if (event.type === TouchType.Move) {
          this.y = Number(event.touches[0].y.toString()).toString();
          this.radius = Number(this.y) / 10;
        }
        if (event.type === TouchType.Up) {
          this.radius = 0;
          this.y = '手指离开屏幕';
        }
      }
      this.text = "按住屏幕上下滑动\n" + "当前手指所在y轴位置 : " + this.y +
      "\n" + "当前图片模糊程度为 : " + this.radius;
    })
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-04 16:23:32
相关问题
组件如何设置模糊效果
2299浏览 • 1回复 待解决
图片模糊效果如何实现
1341浏览 • 1回复 待解决