未知动画名称:decelerateInterpolator
Unknown animation name: decelerateInterpolator
Android Studio 1.5
Device Samsung 4.4.2
我正在尝试对从 ArrayList 加载到 recyclerview 的项目进行动画处理。当单击下拉箭头时,项目在展开时应该动画(减速)并且在折叠时应该动画。但是,目前列表项只是出现。
调用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);
非常感谢您的任何建议,
你不能使用decelerate_interpolator
因为它不是动画,它是一个插值器:
An interpolator defines the rate of change of an animation. This
allows the basic animation effects (alpha, scale, translate, rotate)
to be accelerated, decelerated, repeated, etc.
Reference:
http://developer.android.com/reference/android/view/animation/Interpolator.html
如你所见XML描述的完全不同:
Source 共 decelerate_interpolator.xml
:
<decelerateInterpolator />
Source,共 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>
如果你想使用减速插值器 你需要将它设置为插值器,而不是动画器:
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。
从库的官方文档中,清楚地说明了如何创建 expand/collapse 动画:
You can also create your own animations for expansion by overriding
ParentViewHolder#onExpansionToggled(boolean)
, which will be called for
you when the itemView is expanded or collapsed.
建议你看一下图书馆的official example
要在列表展开和折叠时设置动画,请考虑使用来自 android 的 ItemAnimator。
您需要设置一个自定义的 itemAnimator,类似于下面 link 中的那个:
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(新的 MyItemAnimator());
您可以使用下面的代码来做到这一点。
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 方法。
希望对您有所帮助。
Android Studio 1.5
Device Samsung 4.4.2
我正在尝试对从 ArrayList 加载到 recyclerview 的项目进行动画处理。当单击下拉箭头时,项目在展开时应该动画(减速)并且在折叠时应该动画。但是,目前列表项只是出现。
调用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);
非常感谢您的任何建议,
你不能使用decelerate_interpolator
因为它不是动画,它是一个插值器:
An interpolator defines the rate of change of an animation. This allows the basic animation effects (alpha, scale, translate, rotate) to be accelerated, decelerated, repeated, etc.
Reference:
http://developer.android.com/reference/android/view/animation/Interpolator.html
如你所见XML描述的完全不同:
Source 共 decelerate_interpolator.xml
:
<decelerateInterpolator />
Source,共 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>
如果你想使用减速插值器 你需要将它设置为插值器,而不是动画器:
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。
从库的官方文档中,清楚地说明了如何创建 expand/collapse 动画:
You can also create your own animations for expansion by overriding
ParentViewHolder#onExpansionToggled(boolean)
, which will be called for you when the itemView is expanded or collapsed.
建议你看一下图书馆的official example
要在列表展开和折叠时设置动画,请考虑使用来自 android 的 ItemAnimator。
您需要设置一个自定义的 itemAnimator,类似于下面 link 中的那个:
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(新的 MyItemAnimator());
您可以使用下面的代码来做到这一点。
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 方法。
希望对您有所帮助。