欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android SpringAnimation弹性动画解析

程序员文章站 2023-11-11 09:27:52
也许你想在android上实现这种反弹的动画效果。android support library 25.3.0引入了dynamic-animation增强动画,里面提供了几...

也许你想在android上实现这种反弹的动画效果。android support library 25.3.0引入了dynamic-animation增强动画,里面提供了几个类用于使动画呈现实现真实的物理效果。

Android SpringAnimation弹性动画解析

你会想,自己的动画里加上 bounceinterpolator或overshootinterpolator 插值器也能达到这种效果,然而实际上达不到。当然你也可以自己写插值器,如果你不嫌麻烦的话。

springanimation弹性动画实现方法

gradle引入,最低支持api16

dependencies {
 compile 'com.android.support:support-dynamic-animation:25.3.0'
}

定义springforce,定义弹性特质

springforce spring = new springforce(finalposition);
spring.setstiffness(stiffness);
spring.setdampingratio(dampingratio);

定义springanimation,并关联springforce对象

springanimation animation = new springanimation(view, property);
animation.setspring(spring);

代码如下

positionactivity.java

public class positionactivity extends appcompatactivity {

 float stiffness = springforce.stiffness_medium;//硬度
 float damping_ratio = springforce.damping_ratio_high_bouncy;//阻尼

 springanimation xanimation;//x方向
 springanimation yanimation;//y方向

 view movingview;//图片

 float dx = 0f;
 float dy = 0f;

 @override
 public void oncreate(@nullable bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_position);

  movingview = findviewbyid(r.id.movingview);

  // 以图片的初始位置创建动画对象
  movingview.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() {
   @override
   public void ongloballayout() {
    xanimation = createspringanimation(
      movingview, springanimation.x, movingview.getx(), stiffness, damping_ratio);
    yanimation = createspringanimation(
      movingview, springanimation.y, movingview.gety(), stiffness, damping_ratio);
    //初始位置确定,移除监听
    movingview.getviewtreeobserver().removeongloballayoutlistener(this);
   }
  });

  movingview.setontouchlistener(new view.ontouchlistener() {
   @override
   public boolean ontouch(view view, motionevent event) {
    switch (event.getactionmasked()) {
     case motionevent.action_down:
      // 计算到左上角的距离
      dx = view.getx() - event.getrawx();
      dy = view.gety() - event.getrawy();

      // 取消动画以便按住图片
      xanimation.cancel();
      yanimation.cancel();
      break;
     case motionevent.action_move:
      // 另一种改变view的layoutparams(位置)的方式
      movingview.animate()
        .x(event.getrawx() + dx)
        .y(event.getrawy() + dy)
        .setduration(0)
        .start();
      break;
     case motionevent.action_up:
      xanimation.start();
      yanimation.start();
      break;
    }
    return true;
   }
  });

 }

 /**
  * 创建弹性动画
  * @param view 动画关联的控件
  * @param property 动画作用的属性
  * @param finalposition 动画结束的位置
  * @param stiffness 硬度
  * @param dampingratio 阻尼
  * @return
  */
 springanimation createspringanimation(view view,
           dynamicanimation.viewproperty property,
           float finalposition,
           @floatrange(from = 0.0) float stiffness,
           @floatrange(from = 0.0) float dampingratio) {
  //创建弹性动画类springanimation
  springanimation animation = new springanimation(view, property);
  //springforce类,定义弹性特质
  springforce spring = new springforce(finalposition);
  spring.setstiffness(stiffness);
  spring.setdampingratio(dampingratio);
  //关联弹性特质
  animation.setspring(spring);
  return animation;
 }

}

activity_position.xml

<?xml version="1.0" encoding="utf-8"?>
<framelayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".positionactivity">

 <imageview
  android:id="@+id/movingview"
  android:layout_width="128dp"
  android:layout_height="128dp"
  android:layout_gravity="center"
  android:src="@drawable/android"
  android:tint="@color/colorprimary"
  tools:ignore="contentdescription"/>

</framelayout>

触摸改变图片的位置,松开手启动动画。

翻译自,原作者使用kotlin语言实现的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。