未知动画名称:decelerateInterpolator

9
Android Studio 1.5
Device Samsung 4.4.2
我试图对从ArrayList加载到recyclerview中的项目进行动画处理。当单击下拉箭头时,项目应该在展开时减速动画(decelerate),在折叠时动画缩小。但是,目前列表项只是出现了。
调用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效果是可以的。 但是,我不想让它们从左侧滑入。

Translated:

我已经测试过以下代码,使用slide_in_left效果是可以的。但是,我不希望它们从左侧滑入。

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

非常感谢任何提供建议的人,


1
你能解释一下你对物品动画的具体要求是什么吗?正如Mattia Maestrini已经指出的那样,使用插值器作为动画毫无意义。 - bonnyz
我可能有一些东西可以帮到你,但只能明天给你答复。请问你的问题是双重的吗?你是在寻求以下两方面的帮助:1. 当顶部的RecyclerView初始加载时,项目必须进行动画处理;2. 当您点击下拉菜单时,出现的项目必须在RecyclerView展开和折叠时进行动画处理。 - Simon
4个回答

7
如果您想使用减速插值器,需要将其设置为插值器,而不是动画器:
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被展开或折叠时会为您调用该方法。

我建议你看一下这个库的官方示例


我尝试了你的代码片段,但它对我没有起作用。我已经更新了我的问题,并附上了一些截图。基本上,我想在RecyclerView展开和折叠时对列表进行动画处理。 - ant2009
@ant2009 能给一些更多的信息吗?例如,发布你正在使用来调用setAnimation()方法的代码。 - bonnyz
我已经更新了我的问题,并添加了所需的代码片段。对于recyclerview,我正在使用com.bignerdranch.android:expandablerecyclerview:2.0.3。 - ant2009
@ant2009,根据您提供的信息,我刚刚更新了我的答案。 - bonnyz

7
您不能使用decelerate_interpolator,因为它不是一个动画,而是一个插值器:
插值器定义了动画的变化率。这使得基本的动画效果(alpha、scale、translate、rotate)可以加速、减速、重复等。
参考:
http://developer.android.com/reference/android/view/animation/Interpolator.html 正如您所看到的,描述它们的XML完全不同: decelerate_interpolator.xml的源代码: Source
<decelerateInterpolator />

slide_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>

2
要想在列表展开和折叠时实现动画效果,建议使用来自Android的ItemAnimator。您需要设置一个自定义的itemAnimator,类似于以下链接中的示例:https://gist.github.com/ademar111190/dc988c8d899dae0193f7。将itemAnimator设置到runPendingAnimations方法中,并使用decelerate interpolator进行平滑过渡。请注意保留HTML标签。
@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 上。

recyclerView.setItemAnimator(new MyItemAnimator());


2
您可以使用以下代码来完成这个任务。
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();
}

你可以在你的按钮上调用这个方法 onClick。希望能对你有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接