动画图像图标从触摸位置到右上角?

Bir*_*dia 3 animation android viewanimator

我正在开发Android onlineShopping应用程序.我必须应用一些动画.

  1. 购物车图像显示在屏幕的右上角.
  2. 屏幕上的每个项目都有"添加到购物车"按钮.
  3. 当用户按下此按钮时,我必须播放动画.
  4. 我有一个修复图像应该从触摸位置动画到放置在屏幕右上角的购物车图像.

请帮帮我.

提前致谢.

更新:

我尝试将图像从一个地方移动到另一个地方.

TranslateAnimation anim = new TranslateAnimation(0,0,200,200);              
                anim.setDuration(3000);

                img.startAnimation(anim);
Run Code Online (Sandbox Code Playgroud)

这个图像我想从触摸位置动画到右上角. 在此输入图像描述

Meh*_*sar 6

最终,您希望使用动画将视图从一个位置移动到另一个位置.

第1步:获取该视图的初始位置

int fromLoc[] = new int[2];
v.getLocationOnScreen(fromLoc);     
float startX = fromLoc[0];
float startY = fromLoc[1];
Run Code Online (Sandbox Code Playgroud)

第2步:获取目的地位置

int toLoc[] = new int[2];
desti.getLocationOnScreen(toLoc);       
float destX = toLoc[0];
float destY = toLoc[1];
Run Code Online (Sandbox Code Playgroud)

第3步:创建一个类来管理动画

        public class Animations {
public Animation fromAtoB(float fromX, float fromY, float toX, float toY, AnimationListener l, int speed){


        Animation fromAtoB = new TranslateAnimation(
                Animation.ABSOLUTE, //from xType
                fromX, 
                Animation.ABSOLUTE, //to xType
                toX, 
                Animation.ABSOLUTE, //from yType 
                fromY, 
                Animation.ABSOLUTE, //to yType 
                toY
                 );

        fromAtoB.setDuration(speed);
        fromAtoB.setInterpolator(new AnticipateOvershootInterpolator(1.0f));


        if(l != null)
            fromAtoB.setAnimationListener(l);               
                return fromAtoB;
    }
}
Run Code Online (Sandbox Code Playgroud)

第4步:添加animationlistener并在所需视图上启动动画

     AnimationListener animL = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {     
        }

        @Override
        public void onAnimationRepeat(Animation animation) {        
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            //this is just a method call you can create to delete the animated view or hide it until you need it again.
            clearAnimation();       
        }
    };
Run Code Online (Sandbox Code Playgroud)

//现在开始动画,如下所述:

Animations anim = new Animations();
    Animation a = anim.fromAtoB(startX, startY, destX, destY, animL,850);
    v.setAnimation(a);
    a.startNow();
Run Code Online (Sandbox Code Playgroud)

我希望它会有所帮助!!