Animate image icon from touch place to right-top corner?

倾然丶 夕夏残阳落幕 提交于 2019-12-19 04:03:48

问题


I am working on Android onlineShopping application.I have to apply some animation.

  1. cart image is displays on Right-top corner of the screen.
  2. List of items are on screen each item with "Add to cart" button.
  3. When user press this button I have to play animation.
  4. I have one fix image which should animate from touch position to cart-image placed on right-top corner of the screen.

Please help me out.

Thanks in advance.

Update :

I Tried this to move image from one place to another.

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

                img.startAnimation(anim);

This image I want to animate from touch position to right-top corner.


回答1:


ultimately you want to move a view from one position to another position with animation.

Step 1: get initial position of that view

int fromLoc[] = new int[2];
v.getLocationOnScreen(fromLoc);     
float startX = fromLoc[0];
float startY = fromLoc[1];

Step 2: get destination position

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

Step 3: create a class to manage animation

        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;
    }
}

Step 4: add animationlistener and start animation on desired view

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

//now start animation as mentioned below:

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 !!




回答2:


I think you are exactly looking for, you can check link

link here




回答3:


check this example hope this will help u: http://developer.android.com/training/animation/zoom.html



来源:https://stackoverflow.com/questions/19534477/animate-image-icon-from-touch-place-to-right-top-corner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!