补间动画的bug

来源:互联网 发布:罗克韦尔plc编程 编辑:程序博客网 时间:2024/04/30 10:17

我们在按追中发现,一般的动画在移动或旋转等操作后,再点击之前位置时,仍能发生效果,这是一个bug,因为一般的动画是补间动画,在动画的位置发生改变时,动画的属性并未发生改变,因此点击动画之前的位置,仍能发生响应,那么怎么解决呢???

这里有两种解决办法,

第一种:利用view和viewGroup的区别,我们通过ViewGrop可以得到子View,然后把子view设置成不可点击

第二种:我们不使用补间动画,使用属性动画


第一种解决办法:

在隐藏的动画中得到子view,并设置子view不可点击,这样在动画发生后,点击动画之前位置时就不会有点击事件的发生

public static void hideView(ViewGroup view, int j) {      RotateAnimation animation = new RotateAnimation(0,180, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1f);        animation.setDuration(500);        animation.setFillAfter(true);        animation.setStartOffset(j);        view.startAnimation(animation);        for (int i=0;i<view.getChildCount();i++) { <span style="font-family: Arial, Helvetica, sans-serif;">//通过得到子view,然后设置子view为可点击</span>            View childView = view.getChildAt(i);            childView.setEnabled(false);        }           }

由于之前在隐藏控件时设置控件不可以点击,因此在显示控件时就要把之前的子view设置为允许发生点击事件

public static void showView(ViewGroup view, int j) {       /RotateAnimation animation = new RotateAnimation(180,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1f);        animation.setDuration(500);        animation.setFillAfter(true);        animation.setStartOffset(j);        view.startAnimation(animation);
        for (int i=0;i<view.getChildCount();i++) {//通过得到子view,然后设置子view为可点击            View childView = view.getChildAt(i);            childView.setEnabled(true);        }    }



第二种解决办法,使用属性动画

public static void hideView(ViewGroup view, int j) {        ObjectAnimator animator = ObjectAnimator.ofFloat(view,"rotation",0,180);//<span style="font-size: 13.3333px; font-family: Arial, Helvetica, sans-serif;">rotation表示的旋转动画,这是设置是发生什么动画的,注意首字母是小写</span>        animator.setDuration(500);//设置动画持续时间        animator.setStartDelay(j);//设置动画延迟时间        animator.start();//开启动画        view.setPivotX(view.getWidth() / 2);//设置旋转动画的横坐标        view.setPivotY(view.getHeight());//设置旋转动画的纵坐标    }




0 0
原创粉丝点击