RecyclerView<第二十一篇>:条目动画之缩放动画(属性
2019-10-23 本文已影响0人
NoBugException
【整体刷新效果】
当第一次显示RecyclerView
的Item或者往下滑动时的动画效果如下:
【刷新指定Item】
刷新指定Item代码:
urls.set(position, "http://img4.imgtn.bdimg.com/it/u=3209370120,2008812818&fm=26&gp=0.jpg");
recycleViewAdapter.notifyItemChanged(position);
【删除效果】
删除刷新代码:
urls.remove(position);
recycleViewAdapter.notifyItemRemoved(position);
recycleViewAdapter.notifyItemRangeChanged(position, urls.size() - position);
【插入效果】
插入刷新代码:
urls.add(position, "http://img4.imgtn.bdimg.com/it/u=3209370120,2008812818&fm=26&gp=0.jpg");
recycleViewAdapter.notifyItemInserted(position);
recycleViewAdapter.notifyItemRangeChanged(position, urls.size() - position);
【移动效果】
移动刷新代码:
int fromPosition = position;
int toPosition = position + 1;
Collections.swap(urls,fromPosition,toPosition);
recycleViewAdapter.notifyItemMoved(fromPosition, toPosition);
recycleViewAdapter.notifyItemRangeChanged(position, toPosition - fromPosition + 1);
缩放动画实现代码如下:
AnimationAdapter.java
public abstract class AnimationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private RecyclerView.Adapter<RecyclerView.ViewHolder> mAdapter;
private int mDuration = 300;
private Interpolator mInterpolator = new LinearInterpolator();
private int mLastPosition = -1;
private boolean isFirstOnly = true;
public AnimationAdapter(RecyclerView.Adapter<RecyclerView.ViewHolder> adapter) {
mAdapter = adapter;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return mAdapter.onCreateViewHolder(parent, viewType);
}
@Override
public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
super.registerAdapterDataObserver(observer);
mAdapter.registerAdapterDataObserver(observer);
}
@Override
public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
super.unregisterAdapterDataObserver(observer);
mAdapter.unregisterAdapterDataObserver(observer);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
mAdapter.onAttachedToRecyclerView(recyclerView);
}
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
mAdapter.onDetachedFromRecyclerView(recyclerView);
}
@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
super.onViewAttachedToWindow(holder);
mAdapter.onViewAttachedToWindow(holder);
}
@Override
public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
mAdapter.onViewDetachedFromWindow(holder);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
mAdapter.onBindViewHolder(holder, position);
int adapterPosition = holder.getAdapterPosition();
if (!isFirstOnly || adapterPosition > mLastPosition) {
for (Animator anim : getAnimators(holder.itemView)) {
anim.setDuration(mDuration).start();
anim.setInterpolator(mInterpolator);
}
mLastPosition = adapterPosition;
} else {
holder.itemView.setScaleX(1);
holder.itemView.setScaleY(1);
}
}
@Override public void onViewRecycled(RecyclerView.ViewHolder holder) {
mAdapter.onViewRecycled(holder);
super.onViewRecycled(holder);
}
@Override
public int getItemCount() {
return mAdapter.getItemCount();
}
public void setDuration(int duration) {
mDuration = duration;
}
public void setInterpolator(Interpolator interpolator) {
mInterpolator = interpolator;
}
public void setStartPosition(int start) {
mLastPosition = start;
}
protected abstract Animator[] getAnimators(View view);
public void setFirstOnly(boolean firstOnly) {
isFirstOnly = firstOnly;
}
@Override
public int getItemViewType(int position) {
return mAdapter.getItemViewType(position);
}
public RecyclerView.Adapter<RecyclerView.ViewHolder> getWrappedAdapter() {
return mAdapter;
}
@Override
public long getItemId(int position) {
return mAdapter.getItemId(position);
}
}
ScaleInAnimationAdapter.java
public class ScaleInAnimationAdapter extends AnimationAdapter {
private static final float DEFAULT_SCALE_FROM = .5f;
private final float mFrom;
public ScaleInAnimationAdapter(RecyclerView.Adapter adapter) {
this(adapter, DEFAULT_SCALE_FROM);
}
public ScaleInAnimationAdapter(RecyclerView.Adapter adapter, float from) {
super(adapter);
mFrom = from;
}
@Override protected Animator[] getAnimators(View view) {
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", mFrom, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", mFrom, 1f);
return new ObjectAnimator[] { scaleX, scaleY };
}
}
使用缩放Adapter
ScaleInAnimationAdapter adapter = new ScaleInAnimationAdapter(recycleViewAdapter);
adapter.setDuration(3000);
adapter.setFirstOnly(false);
recyclerview.setAdapter(adapter);
[本章完...]