android自定义RecyclerView的LayoutManager实现照片环形布局动画交互

先看效果

具体实现,
自定义一个RecyclerView的LayoutManager实现图片发环绕,算法如下

			double angle = Math.toRadians(((double) i) / ((double) num) * 360d);
            int width = usedMaxWidth - childWidth;
            int itemLeft = width / 2 + (int) (Math.cos(angle) * (double) width / 2d);
            int itemHeight = height - childHeight;
            int itemCalcTop = itemHeight / 2 + (int) (Math.sin(angle) * (double) itemHeight / 2d);
            if ((itemLeft > usedMaxWidth / 2 - childWidth - halfBankWidth) && (itemLeft < usedMaxWidth / 2 + halfBankWidth)) {
                if (i > getItemCount() / 2) {
                    minTop = height / 2 + halfBankHeight;
                    maxTop = height - childHeight;
                    minTop = Math.max(minTop, itemCalcTop);
                } else {
                    minTop = 0;
                    maxTop = height / 2 - childHeight - halfBankHeight;
                    maxTop = Math.min(itemCalcTop, maxTop);
                }
            } else {
                if (i > getItemCount()/2){
                    minTop = 0;
                    maxTop = height/2;
                }else {
                    minTop = height/2;
                    maxTop = height - childHeight;
                }
            }

横向线性分布,中间显示图片区域,取布局范围为留空的高度以上或以下,留空布局之外在整个竖向上随机
动画交互 因为每次notifyDataSetChanged()会从新创建条目,所以先用个map在LayoutManager内部存储上次条目所在的位置,map键为条目唯一的标识,如id或唯一的图片地址等,另外定义一个条目变动前后的位置信息

public class PositionTag {
    public int oldX;
    public int oldY;
    public int nowX;
    public int nowY;
}

通过RecyclerView条目动画实现运动交互,在Adapter的onViewAttachedToWindow为RecyclerView添加动画代码实现如下

@Override
    public void onViewAttachedToWindow(BaseViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        addAnimation(holder);
    }

    private void addAnimation(BaseViewHolder holder) {
        for (Animator anim : mSelectAnimation.getAnimators(holder.itemView)) {
            anim.setDuration(1000).start();
            anim.setInterpolator(new LinearInterpolator());
        }
    }

按自己需求定义动画

public Animator[] getAnimators(View view,int translationX,int translationY) {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", mFrom, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", mFrom, 1f);
        ObjectAnimator transX = ObjectAnimator.ofFloat(view, "translationX",   translationX , 0);
        ObjectAnimator transY = ObjectAnimator.ofFloat(view, "translationY", translationY, 0);
        return new ObjectAnimator[]{scaleX, scaleY,transX,transY};
    }

最后通过点击条目,打乱list,实现图片的错乱交互

最后附上,源码地址
点击下载源码

你可能感兴趣的:(自定义RecycleView,图片列表环绕,android,java,kotlin)