【问题标题】:Unknown animation name: decelerateInterpolator未知动画名称:decelerateInterpolator
【发布时间】:2015-11-04 18:30:05
【问题描述】:
Android Studio 1.5
Device Samsung 4.4.2

我正在尝试对从 ArrayList 加载到回收站视图中的项目进行动画处理。当单击下拉箭头时,项目应在展开时动画(减速),在折叠时应动画。但是,目前列表项只是出现。

调用 setAnimation 的代码

 @Override
    public void onBindChildViewHolder(ChatChildViewHolder childViewHolder, int position, Object childListItem) {
        ChatChildTitles chatChildTitles = (ChatChildTitles)childListItem;
        childViewHolder.tvChildTitle.setText(chatChildTitles.getTitle());

        setAnimation(childViewHolder.cvChildRooms, position);
    }

设置动画的代码

  private void setAnimation(CardView viewToAnimate, int position) {
        Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.fade_in);
        animation.setInterpolator(mContext, android.R.anim.decelerate_interpolator);
        viewToAnimate.startAnimation(animation);
    }

这里有几个截图:

处于折叠状态

点击箭头后展开列表

这是我正在使用的布局,它表示将在 recyclerView 中显示的行:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    android:id="@+id/cvChildRooms"
    xmlns:card="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card:cardBackgroundColor="@color/child_header_lighter_grey"
    card:contentPadding="4dp"
    card:cardPreventCornerOverlap="true">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profile_image"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical|start"
        android:src="@drawable/photorace"/>

    <TextView
        android:id="@+id/tvChildTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center"
        android:text="Coffee Latte Room"
        android:fontFamily="sans-serif-light"
        android:textSize="16sp"
        android:textColor="@android:color/black"/>
</android.support.v7.widget.CardView>

我有一个应该启动动画的函数。

private void setAnimation(CardView viewToAnimate, int position) {
    Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.decelerate_interpolator);
    viewToAnimate.startAnimation(animation);
}

我已经测试过使用slide_in_left 可以正常工作的以下内容。但是,我不希望它们从左侧滑入

Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);

非常感谢您的任何建议,

【问题讨论】:

  • 您能具体解释一下您想要为您的物品制作什么样的动画吗?正如 Mattia Maestrini 已经指出的那样,使用插值器作为动画是没有意义的......
  • 我可能有东西给你,但只能明天给你答复。如果你的问题是双重的,你能告诉我吗?您正在寻求帮助 1. 最初加载顶部 recyclerview 时,该项目必须是动画的; 2. 当您单击下拉菜单时,当 recyclerview 展开和折叠时出现的项目必须是动画的?

标签: android android-animation


【解决方案1】:

如果你想使用 减速插值器 你需要将它设置为插值器,而不是动画器

private void setAnimation(CardView viewToAnimate, int position) {
    Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.fade_in); //change this with your desidered (or custom) animation
    animation.setInterpolator(mContext, android.R.anim.decelerate_interpolator);
    viewToAnimate.startAnimation(animation);
}

更新

你说你正在使用com.bignerdranch.android:expandablerecyclerview:2.0.3

从库的官方文档中,清楚地说明了如何创建展开/折叠动画

您还可以通过覆盖创建自己的动画以进行扩展 ParentViewHolder#onExpansionToggled(boolean),将被调用 当 itemView 展开或折叠时。

我建议你看看图书馆的official example

【讨论】:

  • 我试过你的 sn-p 但它对我不起作用。我用一些截图更新了我的问题。基本上,我想在 recyclerview 展开和折叠时为列表设置动画。
  • @ant2009 你能提供更多信息吗?例如,发布您用来调用 setAnimation() 方法的代码
  • 我已使用所需的代码 sn-ps 更新了我的问题。对于 recyclerview,我使用的是 com.bignerdranch.android:expandablerecyclerview:2.0.3
  • @ant2009 我刚刚根据你提供的信息更新了我的答案
【解决方案2】:

你不能使用decelerate_interpolator,因为它不是动画,它是一个插值器:

插值器定义动画的变化率。这个 允许基本动画效果(alpha、缩放、平移、旋转) 加速、减速、重复等。

参考:
http://developer.android.com/reference/android/view/animation/Interpolator.html

如您所见,描述它们的 XML 完全不同:

Sourcedecelerate_interpolator.xml:

<decelerateInterpolator />

Sourceslide_in_left.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-50%p" android:toXDelta="0"
            android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

【讨论】:

    【解决方案3】:

    要在列表展开和折叠时为其设置动画,请考虑使用 android 中的 ItemAnimator。

    您需要设置一个自定义 itemAnimator,类似于下面链接中的那个:

    https://gist.github.com/ademar111190/dc988c8d899dae0193f7

    将 runPendingAnimations 方法中的 itemAnimator 设置为减速插值器。

    @Override
        public void runPendingAnimations() {
            if (!mViewHolders.isEmpty()) {
                int animationDuration = 300;
                AnimatorSet animator;
                View target;
                for (final RecyclerView.ViewHolder viewHolder : mViewHolders) {
                    target = viewHolder.itemView;
                    target.setPivotX(target.getMeasuredWidth() / 2);
                    target.setPivotY(target.getMeasuredHeight() / 2);
    
                    animator = new AnimatorSet();
    
                    animator.playTogether(
                            ObjectAnimator.ofFloat(target, "translationX", -target.getMeasuredWidth(), 0.0f),
                            ObjectAnimator.ofFloat(target, "alpha", target.getAlpha(), 1.0f)
                    );
    
                    animator.setTarget(target);
                    animator.setDuration(animationDuration);
                    animator.setInterpolator(new DecelerateInterpolator());
                    animator.setStartDelay((animationDuration * viewHolder.getPosition()) / 10);
                    animator.addListener(new AnimatorListener() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            mViewHolders.remove(viewHolder);
                        }
                    });
                    animator.start();
                }
            }
        }
    

    然后您需要将 itemAnimator 设置为回收站视图。

    recyclerView.setItemAnimator(new MyItemAnimator());

    【讨论】:

      【解决方案4】:

      你可以使用下面的代码来做到这一点。

      private void hideViews() {
        recyclerView.animate().translationY(-recyclerView.getHeight()).setInterpolator(new AccelerateInterpolator(2));
      
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mFabButton.getLayoutParams();
        int fabBottomMargin = lp.bottomMargin;
        mFabButton.animate().translationY(mFabButton.getHeight()+fabBottomMargin).setInterpolator(new AccelerateInterpolator(2)).start();
      }
      
      private void showViews() {
        recyclerView.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));
        mFabButton.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
      }
      

      您可以在您的 Button 中调用此方法 onClick

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-17
        • 2013-07-19
        • 1970-01-01
        • 2014-11-15
        • 1970-01-01
        • 2010-12-17
        • 2015-11-12
        • 1970-01-01
        相关资源
        最近更新 更多