当前位置:  开发笔记 > 编程语言 > 正文

展开/折叠动画:小滞后|| MeasureSpec返回错误的值

如何解决《展开/折叠动画:小滞后||MeasureSpec返回错误的值》经验,为你挑选了0个好方法。

我用的是下面两种方法(启发/从这里复制)到expandcollapse一些TextViewsScrollView通过点击"头" - TextView.

伪布局结构:


    
        
            
        
        
        
        
        
        
        
    

分频器很简单View,height设置为1dp.内容TextViews风格包括:

    0dp
    match_parent

和一些边距和填充.

方法在这里:

public static void expand(final View v) {

    //v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    int matchParentMeasureSpec = View.MeasureSpec.makeMeasureSpec(((View) v.getParent()).getWidth(), View.MeasureSpec.EXACTLY);
    int wrapContentMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    v.measure(matchParentMeasureSpec, wrapContentMeasureSpec);

    final int targetHeight = v.getMeasuredHeight();

    // Older versions of android (pre API 21) cancel animations for views with a height of 0.
    v.getLayoutParams().height = 1;
    v.setVisibility(View.VISIBLE);
    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1
                    ? ViewGroup.LayoutParams.WRAP_CONTENT
                    : (int) (targetHeight * interpolatedTime);
            scrollView.smoothScrollTo(0, (int) (targetHeight * interpolatedTime));
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setInterpolator(easeInOutQuart);
    a.setDuration(computeDurationFromHeight(v));
    v.startAnimation(a);

}

public static void collapse(final View v) {
    final int initialHeight = v.getMeasuredHeight();

    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if (interpolatedTime == 1) {
                v.setVisibility(View.GONE);
            } else {
                v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setInterpolator(easeInOutQuart);
    a.setDuration(computeDurationFromHeight(v));
    v.startAnimation(a);
}

private static int computeDurationFromHeight(View view) {
    // 1dp/ms * multiplier
    return (int) (view.getMeasuredHeight() / view.getContext().getResources().getDisplayMetrics().density) * 4;
}

这里的问题:一切正常 - 直到expand animation达到文本的最后一行 - 如果它上面太少characters,那么它会滞后,跳跃,爆炸? - 但是你要打电话 - 直到完全展开.

Collapsing 似乎工作正常.

我尝试了其他Interpolator值,方法中的另一个乘数computeDurationFromHeight.

一些测试:

4行,在第四行,超过17个字符工作正常,少于18个字符,它滞后.

最后一行的3行和无关数量的字符工作正常.

有时候animation首先是作品expand,而不是第二件作品.

似乎TextView计算错了.multiplier我看到一些高text点,在下一个标题上下跌了0.5秒TextView

删除smoothScrollToin expand不会改变任何东西(当然滚动除外..)

其他内插器也有"打嗝",但更短

重要:

一些登录applyTransformation(见下文)让我明白了,我看到它final height打印了两次 - 恰好有50点(像素?dp?)的差异.//smoothly increasing height and then: final height = 202 height = 252 final height = 252虽然我得到了targetHeight = 203- 所以height首先计算错误,然后发生一些魔法?


@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    v.getLayoutParams().height = interpolatedTime == 1
                    ? ViewGroup.LayoutParams.WRAP_CONTENT
                    : (int) (targetHeight * interpolatedTime);
    v.requestLayout();

    scrollView.smoothScrollTo(0, interpolatedTime == 1
                    ? v.getHeight() : (int) (targetHeight * interpolatedTime));

    Log.d("Anim", "height = " + v.getHeight());
    if (interpolatedTime == 1){
        Log.d("Anim", "final height = " + v.getHeight());
    }
}

任何人都可以指出我错过了什么?

推荐阅读
mobiledu2402851373
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有