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

3
我正在开发一款Android在线购物应用程序,需要应用一些动画效果。
以下是具体需求:
  1. 购物车图片显示在屏幕右上角。
  2. 商品列表显示在屏幕上,每个商品都有一个“添加到购物车”的按钮。
  3. 当用户按下此按钮时,我需要播放动画。
  4. 我有一张固定的图片,应从触摸位置动画移动到位于屏幕右上角的购物车图像。
请帮我解决这个问题,谢谢!
更新:
我尝试过将图片从一个位置移动到另一个位置。
TranslateAnimation anim = new TranslateAnimation(0,0,200,200);              
                anim.setDuration(3000);

                img.startAnimation(anim);

我想从触摸位置将这个图片动画化到右上角。 enter image description here


@Indiandroid 我从未涉及过动画。我只看过一个演示,但没有满足要求。 - Biraj Zalavadia
正如您所提到的,您想从ListView项目中制作动画并将其移动到右上角?如果可能的话,请添加屏幕截图。 - TheFlash
@Indiandroid 嗨兄弟,请帮我一下。我无法从动画演示代码片段中找出问题。 - Biraj Zalavadia
@Indiandroid 是的,我可以帮忙,但请添加图片以便我能够理解。 - TheFlash
3个回答

7
最终你希望通过动画将一个视图从一个位置移动到另一个位置。 步骤1:获取该视图的初始位置
int fromLoc[] = new int[2];
v.getLocationOnScreen(fromLoc);     
float startX = fromLoc[0];
float startY = fromLoc[1];

步骤2:获取目标位置

int toLoc[] = new int[2];
desti.getLocationOnScreen(toLoc);       
float destX = toLoc[0];
float destY = toLoc[1];

第三步:创建一个用于管理动画的类。
        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;
    }
}

步骤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();       
        }
    };

//现在按照下面的方法开始动画:

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

I hope it will be helpful !!


谢谢你的好回答,听起来很有前途。我会尝试并更新你这段代码对我的影响。 - Biraj Zalavadia

1
我认为您正在寻找的内容就在这里,您可以查看链接:

链接在此处

enter image description here


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接