android任意view移动缩放至消失到任何位置的通用动画

要求将界面中显示的任意一个view(已经Measure好了)缩放移动到任意位置直至消失

可以用图片这样描述:
这里写图片描述

实现思路:使用系统缩放动画ScaleAnimation进行缩放和移动,缩放倍数和移动距离根据移动的view的xy宽高和目标xy计算,任意位置的动画将受到不同的布局限制,但activity有个windows里面包含decorView,decorView里面又包含rootview,decorView不包含通知栏,但包含通知栏的高度,所以选择使用rootview,即xml布局的上一层FrameLayout布局,你可以在rootview(通过getwindows().getdecorview.findViewById(android.R.id.content)或 activity.getWindow().getDecorView().getRootView();获得)添加移动的view,便可以在任意位置移动

代码实现:

 public static void animToTagOnWindows (Activity activity,View tagView,View toView,float scale) {
        int[] toXY = new int[2];
        toView.getLocationOnScreen(toXY);

        int centerX = (int) (toXY[0] + toView.getMeasuredWidth()/2f);
        int centerY = (int) (toXY[1] + toView.getMeasuredHeight()/2f);
       animToTagOnWindows(activity,tagView,centerX,centerY,scale);
    }

    public static void animToTagOnWindows (Activity activity, View tagView, int toCenterX, int toCenterY, float scale) {
        int[] winXY = new int[2];
        tagView.getLocationOnScreen(winXY);

        float toX = tagView.getMeasuredWidth()*scale;
        float toY = tagView.getMeasuredHeight()*scale;
        float pivotX = (toCenterX-winXY[0])*1f/tagView.getMeasuredWidth();
        float pivotY = (toCenterY-winXY[1])*1f/tagView.getMeasuredHeight();

        ScaleAnimation scaleAnimation =new ScaleAnimation(1.0f,toX, 1f, toY, Animation.RELATIVE_TO_SELF,pivotX, Animation.RELATIVE_TO_SELF, pivotY);

        final ImageView tempMoveView = new ImageView(activity);
        tempMoveView.setScaleType(ImageView.ScaleType.FIT_XY);
        Bitmap tempBm = getViewBitmap(tagView);
        tempMoveView.setImageBitmap(tempBm);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(tagView.getMeasuredWidth(), tagView.getMeasuredHeight());
        params.setMargins(winXY[0],winXY[1],winXY[0] +tagView.getMeasuredWidth(),winXY[1]+tagView.getMeasuredHeight());
        tempMoveView.setLayoutParams(params);

        final FrameLayout frameLayout = (FrameLayout) activity.getWindow().getDecorView().getRootView();
        frameLayout.addView(tempMoveView);

        scaleAnimation.setDuration(1000);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //移除临时显示动画的view
                frameLayout.removeView(tempMoveView);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        tempMoveView.startAnimation(scaleAnimation);
    }

博客出处

最终效果如下:
这里写图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值