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) 前执行,否则会出错~
    至此大功告成~
  • 相关阅读:
    scratch资源
    如何把scratch转成一个swf文件或者exe执行文件
    perl的匿名引用
    perl的内置函数scalar
    perl内置特殊变量查询
    Win7中安装Windows PowerShell 3.0
    man-pages项目包含文档的说明
    编译器思维之结合律
    Android中利用jsoup解析html页面
    MVP+Retrofit+RxJava
  • 原文地址:https://www.cnblogs.com/eoiioe/p/2662546.html
Copyright © 2011-2022 走看看