如何在图像内的任意点上旋转(以及可能的动画)图像?

2024-03-28

我正在使用 Flutter,并且希望图像在我定义的点上旋转。例如,我希望图像在其右上角设置旋转(向下摆动)的动画。我该怎么做呢?


这是一个使用以下方法的解决方案FractionalOffset https://docs.flutter.io/flutter/painting/FractionalOffset-class.html类来指定旋转的点。

如果你不想动画化Transform https://docs.flutter.io/flutter/widgets/Transform-class.html做你想做的事。

    return new Transform(
      transform: new Matrix4.rotationZ(math.PI),
      alignment: FractionalOffset.bottomRight,
      child: child,
    );

如果你确实想制作动画,RotationTransition https://docs.flutter.io/flutter/widgets/RotationTransition-class.html几乎可以满足您的要求,但对齐方式不可配置。您可以制作自己的可配置版本:

import 'dart:ui';
import 'dart:math' as math;
import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
      title: "Rotation Demo",
      home: new RotateDemo(),
  ));
}

/// Animates the rotation of a widget around a pivot point.
class PivotTransition extends AnimatedWidget {
  /// Creates a rotation transition.
  ///
  /// The [turns] argument must not be null.
  PivotTransition({
    Key key,
    this.alignment: FractionalOffset.center,
    @required Animation<double> turns,
    this.child,
  }) : super(key: key, listenable: turns);

  /// The animation that controls the rotation of the child.
  ///
  /// If the current value of the turns animation is v, the child will be
  /// rotated v * 2 * pi radians before being painted.
  Animation<double> get turns => listenable;

  /// The pivot point to rotate around.
  final FractionalOffset alignment;

  /// The widget below this widget in the tree.
  final Widget child;

  @override
  Widget build(BuildContext context) {
    final double turnsValue = turns.value;
    final Matrix4 transform = new Matrix4.rotationZ(turnsValue * math.PI * 2.0);
    return new Transform(
      transform: transform,
      alignment: alignment,
      child: child,
    );
  }
}

class RotateDemo extends StatefulWidget {
  State createState() => new RotateDemoState();
}

class RotateDemoState extends State<RotateDemo> with TickerProviderStateMixin {
  AnimationController _animationController;

  @override initState() {
    super.initState();
    _animationController = new AnimationController(
      duration: const Duration(milliseconds: 3000),
      vsync: this,
    )..repeat();
  }

  @override dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body:
          new Center(
            child: new PivotTransition(
              turns: _animationController,
              alignment: FractionalOffset.bottomRight,
              child: new Container(
                decoration: new BoxDecoration(backgroundColor: Colors.grey.shade200),
                width: 100.0,
                child: new FlutterLogo(style: FlutterLogoStyle.horizontal),
              ),
            ),
          ),
    );
  }
}

此示例围绕右下角旋转 Flutter 徽标。

如果您喜欢冒险,您可以向 Flutter 发送拉取请求以进行RotationTransition的对齐方式是可配置的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在图像内的任意点上旋转(以及可能的动画)图像? 的相关文章

随机推荐