一个简单的动画需要注意的坑

来源:互联网 发布:玩名堂换域名吗? 编辑:程序博客网 时间:2024/06/08 16:31

注意此篇文章只是为了记录一个没有找到原因的问题,希望哪位看到可以交流下,谢谢,希望各位遇到相同的问题可以避免异常

关于属性动画应该是很容易入实现的,可以通过以下两种方式

1.自定义一个属性类ValueAnimator,监听属性的参数变化并修改状态,设定时间和执行方式,并执行

//顶部的文字下拉ValueAnimator refreshTvPullAnimator = ValueAnimator.ofInt(0, 100);refreshTvPullAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {    @Override    public void onAnimationUpdate(ValueAnimator animation) {        int value = (int) animation.getAnimatedValue();        mPullDownTv.layout(mPullDownTv.getLeft(), value, mPullDownTv.getLeft() + mPullDownTv.getWidth(), mPullDownTv.getHeight() + value);        if (value == 100) {            //到最后隐藏            mPullDownTv.setVisibility(View.GONE);            //显示左右的两个图标            mLeftIv.setVisibility(VISIBLE);            mRightIv.setVisibility(VISIBLE);        }    }});refreshTvPullAnimator.setDuration(500);refreshTvPullAnimator.setRepeatCount(0);refreshTvPullAnimator.start();

2.自定义一个属性类ObjectAnimator传入需要的变化的参数,设定时间和执行方式,并执行

ObjectAnimator moveAnimatorLeft = ObjectAnimator.ofFloat(mLeftIv, "translationX", 0, 150);moveAnimatorLeft.setDuration(1000);moveAnimatorLeft.setRepeatMode(ValueAnimator.REVERSE);moveAnimatorLeft.setRepeatCount(1);moveAnimatorLeft.start();
关键是使用方法一会出现下面的问题,暂时没有找到原因,不得已只能使用方法二了。
在下面的布局中获取到的控件的位置不准确,最外层是RelativeLayout,尝试了内部是各种控件的情况,甚至是直接布局的情况。
均会出现下面的情况。
出现一个非常的数,考虑这是一个带有标记的数标记了相对位置的信息,但是查看源码也没找到相关的转换方式
下面是获取控件位置的函数的示意图
参考来源:点击打开链接