buildRandowMatrix4 method
lib/demo/shake/shake_animation_builder.dart
构建随机变换的矩阵
animation.value
同时要适配旋转,
Matrix4的旋转是使用弧度计算的,一般抖动使用 0.1左右的弧度微旋转即可
所以这时配置的animation.value
的取值范围建议使用 -0.1,0.1
那么对于Matrix4的translationValues平移来讲是使用的逻辑像素
-0.1,0.1
这个范围的变动对于平移无法有明显的抖动效果
所以在这里 对于平移来说使用的 -1.5,1.5
就会有明显一点的抖动效果
random.nextDouble()
这个方法的值范围为 0.0-1.0
然后通过结合配置的randomValue抖动的波动范围 默认为 5
Matrix4平移范围为 -1.5,6.5
Implementation
Matrix4 buildRandowMatrix4() {
///随机数
int nextRandom = random.nextInt(10);
///Matrix4矩阵偏移量
double dx = 0;
double dy = 0;
if (nextRandom % 4 == 0) {
///左右平移
dx = animation.value * 15 + randomValue * random.nextDouble();
return Matrix4.translationValues(dx, dy, 0);
} else if (nextRandom % 4 == 1) {
///上下平移
dy = animation.value * 15 + randomValue * random.nextDouble();
return Matrix4.translationValues(dx, dy, 0);
} else if (nextRandom % 4 == 2) {
///对角线平移
dx = animation.value * 15 + randomValue * random.nextDouble();
dy = animation.value * 15 + randomValue * random.nextDouble();
return Matrix4.translationValues(dx, dy, 0);
} else {
///旋转
return Matrix4.rotationZ(animation.value);
}
}