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

android – 展开折叠动画:小滞后|| MeasureSpec返回错误的值,第1张

概述我使用以下两种方法( inspired/copied from here)通过点击“标题”-TextView来展开和折叠ScrollView中的一些TextView. 伪布局结构: <ScrollView> <LinearLayout> <LinearLayout> <!-- some other stuff here --> </L 我使用以下两种方法( inspired/copied from here)通过点击“标题”-TextVIEw来展开和折叠ScrollVIEw中的一些TextVIEw.

伪布局结构:

<ScrollVIEw>    <linearLayout>        <linearLayout>            <!-- some other stuff here -->        </linearLayout>        <TextVIEw "header1"/>        <VIEw "fancydivIDer"/>        <TextVIEw "content1">        <TextVIEw "header2"/>        <VIEw "fancydivIDer"/>        <TextVIEw "content2">    </linearLayout></ScrollVIEw>

divIDer是一个简单的VIEw,heightset为1dp. content-TextVIEws的内容包括:

<item name="androID:layout_height">0dp</item>    <item name="androID:layout_wIDth">match_parent</item>

和一些保证金填充.

方法:

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;}

问题在这里:一切正常 – 直到展开动画到达文本的最后一行 – 如果它的字符太少,那么它滞后,跳跃,爆炸? – 但是您要调用它 – 直到完全展开.

崩溃似乎工作正常.

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

一些测试:

> 4行,第四行全部17个以上的字符工作正常,少于18个字符,并且滞后.
> 3行和不相关的字数在最后一行工作正常.
>有时动画首先展开,但不是第二次.
>看来,TextVIEw的计算错误.有一个高乘数,我看到一些文本翻转起来,在下一个标题TextVIEw中0.5s
>在expand中删除smoothScrollTo不会改变任何东西(除了滚动当然..)
>其他内插器也有“打嗝”,但更短

重要:

>一些登录applytransformation(见下文)让我指出,我看到最后的高度打印了两次 – 只有50点(像素?dp?)的差异. //顺利增加高度然后:
最终高度= 202高度= 252最终高度= 252当我得到targetHeight = 203 – 所以高度先计算错误,但是然后一些魔法发生?

@OverrIDeprotected 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());    }}

有人可以指出我失踪了吗?

解决方法 这可能是因为扩展最后一个元素可能会触发滚动,因为布局的高度越来越大,因此将显示“推”

尝试在底部添加一个FrameLayout,高度为20dp,如果有任何差异,请尝试观察

总结

以上是内存溢出为你收集整理的android – 展开/折叠动画:小滞后|| MeasureSpec返回错误的值全部内容,希望文章能够帮你解决android – 展开/折叠动画:小滞后|| MeasureSpec返回错误的值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1131715.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存