如何旋转图像内的任意点上的图像(并进行动画处理)? [英] How do I rotate (and possible animate) an image on an arbitrary point inside the image?

查看:104
本文介绍了如何旋转图像内的任意点上的图像(并进行动画处理)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

以下是使用



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


I'm using Flutter, and I'd like an image to rotate on a point I define. For example, I'd like the image to animate the rotation (swing down) on its upper right corner. How do I do this?

解决方案

Here is a solution that uses the FractionalOffset class to specify the point to rotate around.

If you don't want to animate, Transform does what you want.

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

If you do want to animate, RotationTransition almost does what you want, except the alignment isn't configurable. You can make your own version that is configurable:

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),
              ),
            ),
          ),
    );
  }
}

This example rotates the Flutter logo around it's bottom right corner.

If you're feeling adventurous, you could send Flutter a pull request to make the RotationTransition's alignment configurable.

这篇关于如何旋转图像内的任意点上的图像(并进行动画处理)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆