首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android平移动画闪烁问题

android平移动画闪烁问题

作者头像
夏洛克的猫
发布2018-10-18 14:21:58
2K0
发布2018-10-18 14:21:58
举报
文章被收录于专栏:移动开发移动开发

当我们应用android平移动画时,一般会给动画一个监听,当动画结束时,会将view的位置重新绘制到我们想要的位置,因为平移动画并没与真的改变控件的实际位置. 代码如下:

      Animation animation = new TranslateAnimation(0, 0, 0, -mTop);
      animation.setDuration(ANI_TIME);
      animation.setAnimationListener(new Animation.AnimationListener() {
                                    @Override
                                    public void onAnimationStart(Animation animation) {

                                    }

                                    @Override
                                    public void onAnimationEnd(Animation animation) {
                                         //动画结束后更新view到终点位置
                                         mTopView.layout(left, top, right, bottom);
                                    }


                                    @Override
                                    public void onAnimationRepeat(Animation animation) {

                                    }
                                });
      mTopView.startAnimation(animation);                          

但是实际使用的时候,当更新实际位置的时候,view会有跳动,在stackoverflow中有人贴出了解决方案 代码如下:

      Animation animation = new TranslateAnimation(0, 0, 0, -mTop);
      animation.setDuration(ANI_TIME);
      animation.setAnimationListener(new Animation.AnimationListener() {
                                    @Override
                                    public void onAnimationStart(Animation animation) {

                                    }

                                    @Override
                                    public void onAnimationEnd(Animation animation) {

         //防止跳动                            
        TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
        animation.setDuration(1);
        mTopView.startAnimation(animation);
                                         //动画结束后更新view到终点位置
                                         mTopView.layout(left, top, right, bottom);
                                    }


                                    @Override
                                    public void onAnimationRepeat(Animation animation) {

                                    }
                                });
      mTopView.startAnimation(animation);    

在实际写的时候,偶然发现另一种方式也是有效的,不过这种有点违反直觉,就是倒着写动画,先把view更新到终点位置,代码如下:

      //由于更新到终点位置,坐标参考以终点为参考系
      Animation animation = new TranslateAnimation(0, 0, mTop, 0);
      animation.setDuration(ANI_TIME);
      mTopView.startAnimation(animation);
      //更新到终点位置
      mTopView.layout(left, top, right, bottom);     

但是,为何出现view跳动的原因一直没找到分析的文章,若有人知道望请告知.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年06月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档