HarmonyOS 图片旋转角度问题

需求是一根手指拖动图片,图片跟随手指做圆周运动如果rotationGesture支持一根手指,则可以快速实现这个需求。

如果API暂时实现不了,能不能提供一个能实现这个功能的demo?

HarmonyOS
2024-12-24 16:05:28
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

rotationGesture暂时无法实现一指,可以参照如下示例代码进行修改。

代码如下:

import display from '@ohos.display'

function atanToDegrees(atanResult: number) {
  const degrees = atanResult * (180 / Math.PI);
  return degrees < 0 ? degrees + 360 : degrees;
}

function calculateAngleBetweenPoints(p0: Position = {x: 0, y: 0}, p1: Position= {x: 0, y: 0}, p2: Position= {x: 0, y: 0}): number {
  const startY = Number(p1.y) - Number(p0.y)
  const startX = Number(p1.x) - Number(p0.x)
  const startRadians: number = Math.atan2(startY, startX) // 通过反正切函数得到的弧度值
  // 相对于正x轴, 角度范围是0到360度
  const startDeg = atanToDegrees(startRadians)
  const endY = Number(p2.y) - Number(p0.y)
  const endX = Number(p2.x) - Number(p0.x)
  const endRadians: number = Math.atan2(endY, endX)
  const endDeg = atanToDegrees(endRadians)
  return endDeg - startDeg
}

@Entry
@Component
struct SwipeGestureExample {
  @State rotateAngle: number = 0

  @State starPosition: Position = {}
  @State updatePosition: Position = {}
  @State circleCenterPoint: Position = {} // 图片中心点坐标
  private screenH: number = 0
  private screenW: number = 0
  private speed: number = 0

  aboutToAppear(): void {
    let displayClass: display.Display | null = null
    displayClass = display.getDefaultDisplaySync()
    this.screenH = px2vp(displayClass.height)
    this.screenW = px2vp(displayClass.width)
    this.circleCenterPoint = { x: this.screenW / 2, y: this.screenH / 2 }
  }

  build() {
    Column() {
      Stack() {
        Image($r("app.media.HOBITO"))
          .width(100)
          .height(100)
          .objectFit(ImageFit.Contain)
          .rotate({
            centerX: '50%',
            centerY: "50%",
            angle: this.rotateAngle
          })

        Circle()
          .width(300)
          .height(300)
          .fillOpacity(0)
          .strokeWidth(2)
          .stroke(Color.Red)
          .onTouch((event: TouchEvent) => {
            if (event.type === TouchType.Down) {
              this.starPosition = { x: event.touches[0].windowX, y: event.touches[0].windowY }
            } else if (event.type === TouchType.Move) {
              this.updatePosition = { x: event.touches[0].windowX, y: event.touches[0].windowY }
            }
            this.rotateAngle = calculateAngleBetweenPoints(this.circleCenterPoint, this.starPosition, this.updatePosition)
            console.log(this.rotateAngle+"==========")

          })
      }
      .hitTestBehavior(HitTestMode.Transparent)
    }
    .alignItems(HorizontalAlign.Center)
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height('100%')
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-24 18:31:06
相关问题
HarmonyOS 如何获取屏幕旋转角度
765浏览 • 1回复 待解决
Canvas 中 fillText 如何旋转角度
1203浏览 • 1回复 待解决
HarmonyOS 如何获取图片角度
560浏览 • 1回复 待解决
HarmonyOS 手势响应不同角度问题
755浏览 • 1回复 待解决
如何实现图片裁剪、旋转
1405浏览 • 1回复 待解决
HarmonyOS 拍摄视频旋转问题
484浏览 • 1回复 待解决
HarmonyOS Image配置的图片如何旋转
578浏览 • 1回复 待解决
HarmonyOS获取图片旋转值一直报错
1194浏览 • 1回复 待解决
HarmonyOS 旋转设备获取设备方向问题
845浏览 • 1回复 待解决
HarmonyOS 相机旋转横屏拍照问题
1084浏览 • 1回复 待解决
HarmonyOS 组件旋转
624浏览 • 1回复 待解决
HarmonyOS pixelMap旋转
589浏览 • 1回复 待解决