两个RecyclerView中数据跳转显示移动动画

2022-10-18  本文已影响0人  Android14k纯金大佬
    /**
     * @param item      数据实体类
     * @param textView  条目控件
     * @param isSelect  是否是选择了的数据
     */
public void move(NavigationPositionBean item,View textView, boolean isSelect){
        isMove = false;
        final int[] startLocation = new int[2];
        final ImageView moveImageView = getView(textView);
        int[] endLocation = new int[2];
        textView.getLocationInWindow(startLocation);
        //获取终点的坐标
        mRecyclerView.getChildAt(1).getLocationInWindow(endLocation);
        moveAnim(item,moveImageView, startLocation, endLocation,isSelect);
    }
/**
     * 点击item移动动画
     *
     * @param moveView
     * @param startLocation
     * @param endLocation
     */
    private void moveAnim(NavigationPositionBean item,View moveView, int[] startLocation, int[] endLocation, boolean isSelect) {
        int[] initLocation = new int[2];
        //获取传递过来的VIEW的坐标
        moveView.getLocationInWindow(initLocation);
        //得到要移动的VIEW,并放入对应的容器中
        final ViewGroup moveViewGroup = getMoveViewGroup();
        final View mMoveView = getMoveView(moveViewGroup, moveView, initLocation);
        //创建移动动画
        TranslateAnimation moveAnimation = new TranslateAnimation(
                startLocation[0], endLocation[0], startLocation[1],
                endLocation[1]);
        moveAnimation.setDuration(300L);//动画时间
        //动画配置
        AnimationSet moveAnimationSet = new AnimationSet(true);
        moveAnimationSet.setFillAfter(false);//动画效果执行完毕后,View对象不保留在终止的位置
        moveAnimationSet.addAnimation(moveAnimation);
        mMoveView.startAnimation(moveAnimationSet);
        moveAnimationSet.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                moveViewGroup.removeView(mMoveView);
                // 判断点击的是已选位置点 还是待选位置点
                if (isSelect) {
                    viewModel.choiceRefresh(item);
                } else {
                    viewModel.deselectRefresh(item);
                }
                isMove = true;
            }
        });
    }
private ImageView getView(View view) {
        view.destroyDrawingCache();
        view.setDrawingCacheEnabled(true);
        Bitmap cache = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        ImageView iv = new ImageView(this);
        iv.setImageBitmap(cache);
        return iv;
    }

    /**
     * 创建移动的ITEM对应的ViewGroup布局容器
     * 用于存放我们移动的View
     */
    private ViewGroup getMoveViewGroup() {
        //window中最顶层的view
        ViewGroup moveViewGroup = (ViewGroup) getWindow().getDecorView();
        LinearLayout moveLinearLayout = new LinearLayout(Utils.getContext());
        moveLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        moveViewGroup.addView(moveLinearLayout);
        return moveLinearLayout;
    }


    /**
     * 获取移动的VIEW,放入对应ViewGroup布局容器
     * @param viewGroup
     * @param view
     * @param initLocation
     * @return
     */
    private View getMoveView(ViewGroup viewGroup, View view, int[] initLocation) {
        int x = initLocation[0];
        int y = initLocation[1];
        viewGroup.addView(view);
        LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mLayoutParams.leftMargin = x;
        mLayoutParams.topMargin = y;
        view.setLayoutParams(mLayoutParams);
        return view;
    }
上一篇下一篇

猜你喜欢

热点阅读