Animation MatrixTween工作不正常?
我正在和弗利特做一个游戏,发现了这个问题。 虽然动画的开始和结束都正确,但正如您所看到的,在这两者之间的所有内容都是未命中的 它开始下降(因为矩阵中的第一个向量),但即使我将第一个向量完全相同,那么矩阵动画也不会做任何事情,只会在动画以新状态结束后出现 状态代码(我删除了我认为不必要的所有内容,但如果您需要更多信息,我可以粘贴整个类)Animation MatrixTween工作不正常?,animation,dart,tween,flutter,Animation,Dart,Tween,Flutter,我正在和弗利特做一个游戏,发现了这个问题。 虽然动画的开始和结束都正确,但正如您所看到的,在这两者之间的所有内容都是未命中的 它开始下降(因为矩阵中的第一个向量),但即使我将第一个向量完全相同,那么矩阵动画也不会做任何事情,只会在动画以新状态结束后出现 状态代码(我删除了我认为不必要的所有内容,但如果您需要更多信息,我可以粘贴整个类) class\u游戏状态使用TickerProviderStateMixin扩展状态{ 静态矩阵X4原始转换=新矩阵X4.compose( 新向量。向量3(1.0,
class\u游戏状态使用TickerProviderStateMixin扩展状态{
静态矩阵X4原始转换=新矩阵X4.compose(
新向量。向量3(1.0,1.0,1.0),
新向量。四元数。欧拉(0.0,0.0,0.0),
新向量。向量3(1.0,1.0,1.0));
静态矩阵x4动画转换=新矩阵x4.compose(
新向量。向量3(5.0260.0,1.0),
新向量。四元数。欧拉(0.0,1.0,-0.7),
新向量。向量3(0.6,0.6,0.6));
Matrix4 currentMatrix=原始转换;
@凌驾
小部件构建(构建上下文){
归还新脚手架(
正文:新栏目(
mainAxisSize:mainAxisSize.max,
mainAxisAlignment:mainAxisAlignment.end,
儿童:[
新容器(
//应用缺省变换矩阵
变换:当前矩阵,
子项:_buildSquares(),//使用自定义正方形构建GridView
),
_getFooter(),//页脚
],
),
);
}
@凌驾
void initState(){
//初始化动画吐温
animationTween=新矩阵x tween(
开始:原始转换,
结束:动画转换
);
//初始化动画控制器
animationController=新的animationController(
vsync:这个,,
持续时间:新的持续时间(毫秒:800),
)…addListener(){
此.setState(){
currentMatrix=animationTween.evaluate(animationController);
});
});
}
_clickListener(){
//触发动画
animationController.forward(从:0.0);
if(_squares[_currentSquareIndex].state.isClicked()){
_增加分数();
}否则{
_showGameOver();
}
}
}
因此,点击后动画明显开始,矩阵正在改变,但不是以平滑的方式。你知道为什么吗?我错过什么了吗
使用一个正方形的工作示例:
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math_64.dart' as vector;
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin{
static Matrix4 originalTransformation = new Matrix4.compose(
new vector.Vector3(1.0, 1.0, 1.0),
new vector.Quaternion.euler(0.0, 0.0, 0.0),
new vector.Vector3(1.0, 1.0, 1.0));
static Matrix4 animatedTransformation = new Matrix4.compose(
new vector.Vector3(5.0, 260.0, 1.0),
new vector.Quaternion.euler(0.0, 1.0, -0.7),
new vector.Vector3(0.6, 0.6, 0.6));
Matrix4 currentMatrix = originalTransformation;
AnimationController animationController;
Matrix4Tween animationTween;
@override
Widget build(BuildContext context) {
return new Scaffold(
resizeToAvoidBottomPadding: true,
body: new Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
new AnimatedBuilder(
// Pass animation controller
animation: animationController,
builder: (BuildContext context, Widget child) => new Container(
// Apply transformation
transform: animationTween.evaluate(animationController),
child: child,
),
// Passing child argument
child: new GestureDetector(
onTap: _onClick,
child: new Container(
height: 400.0,
width: 400.0,
color: Colors.red,
),
),
),
],
),
);
}
_onClick() {
// Start animation
animationController.forward(from: 0.0);
return;
}
@override
void initState() {
super.initState();
// Init tween for matrix
animationTween = new Matrix4Tween(
begin: originalTransformation,
end: animatedTransformation
);
// Init animation controller
animationController = new AnimationController(
vsync: this,
duration: new Duration(milliseconds: 800),
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:vector\u math/vector\u math\u 64.dart”作为向量;
void main(){
runApp(新的MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“颤振演示”,
主题:新主题数据(
主样本:颜色。蓝色,
),
主页:新建MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key}):超级(Key:Key);
@凌驾
_MyHomePageState createState()=>new_MyHomePageState();
}
类_MyHomePageState使用TickerProviderStateMixin扩展状态{
静态矩阵X4原始转换=新矩阵X4.compose(
新向量。向量3(1.0,1.0,1.0),
新向量。四元数。欧拉(0.0,0.0,0.0),
新向量。向量3(1.0,1.0,1.0));
静态矩阵x4动画转换=新矩阵x4.compose(
新向量。向量3(5.0260.0,1.0),
新向量。四元数。欧拉(0.0,1.0,-0.7),
新向量。向量3(0.6,0.6,0.6));
Matrix4 currentMatrix=原始转换;
动画控制器;
Matrix4Tween animation tween;
@凌驾
小部件构建(构建上下文){
归还新脚手架(
resizeToAvoidBottomPadding:true,
正文:新栏目(
mainAxisSize:mainAxisSize.max,
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
新动画生成器(
//传递动画控制器
动画:animationController,
生成器:(BuildContext上下文,小部件子项)=>新容器(
//应用变换
变换:animationTween.evaluate(animationController),
孩子:孩子,
),
//传递子参数
儿童:新的手势检测器(
onTap:_onClick,
子容器:新容器(
高度:400.0,
宽度:400.0,
颜色:颜色,红色,
),
),
),
],
),
);
}
_onClick(){
//启动动画
animationController.forward(从:0.0);
返回;
}
@凌驾
void initState(){
super.initState();
//矩阵的初始吐温
animationTween=新矩阵x tween(
开始:原始转换,
结束:动画转换
);
//初始化动画控制器
animationController=新的animationController(
vsync:这个,,
持续时间:新的持续时间(毫秒:800),
);
}
}
编辑:我尝试删除侦听器并像这样将动画传递给AnimatedBuilder,但这也不起作用
编辑2:添加了一个正方形的工作示例。传递您的
动画
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math_64.dart' as vector;
class HomeScreen extends StatefulWidget {
HomeScreenState createState() => new HomeScreenState();
}
class HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
AnimationController _controller;
@override initState() {
_controller = new AnimationController(
vsync: this, duration: const Duration(milliseconds: 300));
}
static Matrix4 originalTransformation = new Matrix4.compose(
new vector.Vector3(1.0, 1.0, 1.0),
new vector.Quaternion.euler(0.0, 0.0, 0.0),
new vector.Vector3(1.0, 1.0, 1.0));
static Matrix4 animatedTransformation = new Matrix4.compose(
new vector.Vector3(5.0, 260.0, 1.0),
new vector.Quaternion.euler(0.0, 1.0, -0.7),
new vector.Vector3(0.6, 0.6, 0.6));
Widget build(BuildContext context) {
return new Scaffold(
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.play_arrow),
onPressed: () => _controller.forward(from: 0.0),
),
body: new Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: [
new AnimatedBuilder(
builder: (BuildContext context, Widget child) {
return new Container(
color: Colors.red,
width: 100.0,
height: 100.0,
transform: new Matrix4Tween(
begin: originalTransformation,
end: animatedTransformation
).evaluate(_controller)
);
},
animation: _controller,
),
],
),
);
}
}
class ExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.teal,
),
home: new HomeScreen(),
debugShowCheckedModeBanner: false,
);
}
}
void main() {
runApp(new ExampleApp());
}