In this tutorial, we’ll implement Spring based animations that are a part of the support library in our android application. Spring Animation is part of Android Physics Based animation API.
在本教程中,我们将实现基于Spring的动画,这些动画是我们android应用程序中支持库的一部分。 Spring Animation是Android Physics Based动画API的一部分。
Android Spring动画 (Android Spring Animation)
Android Spring Animation animates views based on spring properties: dampness, stiffness, bouncy.
Android Spring Animation会根据弹簧特性(潮湿度,刚度,弹性)为视图设置动画。
Spring Animation can be implemented in your project once you add the following dependency in your dependencies section in the build.gradle:
只要在build.gradle的“ dependencies”部分中添加以下依赖项,即可在项目中实现Spring Animation:
dependencies {
implementation 'com.android.support:support-dynamic-animation:27.1.1'
}
To create a Spring Animation, we need to create a SpringAnimation class and pass along the animation type – translation/rotation/scaling along with the final position of the view and velocity of the animation.
要创建Spring动画,我们需要创建一个SpringAnimation类并传递动画类型- 平移/旋转/缩放以及动画的最终位置和动画速度。
We can also set additional properties i.e. Stiffness and Damping.
我们还可以设置其他属性,例如Stiffness和Damping 。
A spring in real life bounces when it returns to its final position.
现实生活中的弹簧回到其最终位置时会反弹。
The Higher the Damping and lower the stiffness the more it would oscillate/bounce.
阻尼越高,刚度越低,则其振荡/反弹越多。
To animate a view the final position of the View must always be assigned before.
要为视图设置动画,必须始终在之前分配视图的最终位置。
To start the animation we invoke either start()
or animateToFinalPosition(Float finalPosition)
. The latter updates the final position and calls start() internally.
要启动动画,我们调用start()
或animateToFinalPosition(Float finalPosition)
。 后者更新最终位置并在内部调用start()。
Besides, updateListener and removeListener are the respective listeners for listening to updates and removing the listener when it’s no longer needed.
此外,updateListener和removeListener分别是侦听器,用于侦听更新并在不再需要时删除侦听器。
简单的春天动画 (Simple Spring Animation)
In java, to set spring animation on any view we do:
在Java中,要在任何视图上设置Spring动画,我们可以:
SpringAnimation springAnim = new SpringAnimation(fab, SpringAnimation.TRANSLATION_Y);
SpringForce springForce = new SpringForce();
springForce.setFinalPosition(-200f);
springForce.setStiffness(SpringForce.STIFFNESS_LOW);
springForce.setDampingRatio(SpringForce.DAMPING_RATIO_HIGH_BOUNCY);
springAnim.setSpring(springForce);
springAnim.start();
The position must be a floating value. A negative in the Y direction is upwards. A negative in the X direction is leftwards. fab is the view instance over which the animation happens.
该头寸必须为浮动值。 Y方向的负数是向上的。 X方向的负数向左。 fab是动画发生的视图实例。
像运动一样拖动视图弹簧 (Dragging View Spring Like Movement)
We can also drag a certain view and see it bounce like a spring. For this, we need to set the touch listener on the view.
我们还可以拖动某个视图,然后看到它像春天一样反弹。 为此,我们需要在视图上设置触摸监听器。
private SpringAnimation xAnimation;
private SpringAnimation yAnimation;
ImageView imageView;
private void imageViewDragSpringAnimation() {
imageView.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
imageView.setOnTouchListener(touchListener);
}
private ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
xAnimation = createSpringAnimation(imageView, SpringAnimation.X, imageView.getX(),
SpringForce.STIFFNESS_MEDIUM, SpringForce.DAMPING_RATIO_HIGH_BOUNCY);
yAnimation = createSpringAnimation(imageView, SpringAnimation.Y, imageView.getY(),
SpringForce.STIFFNESS_MEDIUM, SpringForce.DAMPING_RATIO_HIGH_BOUNCY);
}
};
private View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dX = v.getX() - event.getRawX();
dY = v.getY() - event.getRawY();
// cancel animations
xAnimation.cancel();
yAnimation.cancel();
break;
case MotionEvent.ACTION_MOVE:
imag