zoukankan      html  css  js  c++  java
  • Android移动view动画问题

    Android写动画效果不是一般的麻烦,网上找了好久,终于解决了动画的问题,总结记录以共勉。
    仅以水平方向移动效果做说明,垂直方向类似。

    完整动画函数代码: 

     1 public void slideview(final float p1, final float p2) {
     2     TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0);
     3     animation.setInterpolator(new OvershootInterpolator());
     4     animation.setDuration(durationMillis);
     5     animation.setStartOffset(delayMillis);
     6     animation.setAnimationListener(new Animation.AnimationListener() {
     7         @Override
     8         public void onAnimationStart(Animation animation) {
     9         }
    10         
    11         @Override
    12         public void onAnimationRepeat(Animation animation) {
    13         }
    14         
    15         @Override
    16         public void onAnimationEnd(Animation animation) {
    17             int left = view.getLeft()+(int)(p2-p1);
    18             int top = view.getTop();
    19             int width = view.getWidth();
    20             int height = view.getHeight();
    21             view.clearAnimation();
    22             view.layout(left, top, left+width, top+height);
    23         }
    24     });
    25     view.startAnimation(animation);
    26 }
    调用示例: 
    移动到目标位置
    slideview(0, distance);
    从目标位置移回原位

    slideview(0, -distance); 

    过程中遇到的问题:

     1、动画执行完成后,view回到原位

    1 TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0);
    2 animation.setInterpolator(new OvershootInterpolator());
    3 animation.setDuration(durationMillis);
    4 animation.setStartOffset(delayMillis);
    5 view.startAnimation(animation);

    开始时动画效果只写了这么多,发现动画执行完,view会回到原位。

    经过查资料尝试使用animation.setFillAfter(true); view不再返回原位,但又出现了第2个问题


    2、点击按钮时,view在初始位置会先闪一下,再执行动画 

     经过查资料得知,animation.setFillAfter(true); 只是将view移动到了目标位置,但是view绑定的点击事件还在原来位置,导致点击时会先闪一下

    又查资料找到解决办法:
    不加setFillAfter, 通过设置view位置实现效果,增加如下代码
     1 animation.setAnimationListener(new Animation.AnimationListener() {
     2     @Override
     3     public void onAnimationStart(Animation animation) {
     4     }
     5     
     6     @Override
     7     public void onAnimationRepeat(Animation animation) {
     8     }
     9     
    10     @Override
    11     public void onAnimationEnd(Animation animation) {
    12         int left = view.getLeft()+(int)(p2-p1);
    13         int top = view.getTop();
    14         int width = view.getWidth();
    15         int height = view.getHeight();
    16         view.clearAnimation();
    17         view.layout(left, top, left+width, top+height);
    18     }
    19 });

    在动画执行完毕后(onAnimationEnd)设置view的位置,同时要clearAnimation()

    注:clearAnimation() 必须在 layout(l,t,r,b) 前执行,否则会出错~
    至此大功告成~
  • 相关阅读:
    Linux——k8s命令别名修改
    k8s—centos7安装部署NFS服务器和客户端及基于nfs的动态存储storageclass使用总结
    MySQL—用户和权限管控
    MySQL—常用SQL语句整理总结
    Zookeeper——入门介绍(相关原理、安装启动及使用操作)
    SpringBoot——Quartz定时框架的使用详解和总结
    SpringBoot——@Scheduled的自定义周期性线程池解决任务延时执行问题
    Linux—用户新建目录和文件的默认权限设置:umask详解
    设计模式——单例模式详解
    Linux—CPU核数、上下文切换介绍及pidstat等命令详解
  • 原文地址:https://www.cnblogs.com/eoiioe/p/2662546.html
Copyright © 2011-2022 走看看