What's the correct way to animate a View from one coordinate to another?

后端 未结 3 1185
夕颜
夕颜 2021-02-08 04:09

I would like to do the following. I have a set of buttons that have some icons on them. When the user taps one, I would like to introduce a new View that starts in the same coo

3条回答
  •  长发绾君心
    2021-02-08 04:26

    So, it turns out that this is much more straight forward than I imagined.

    I created a full screen RelativeLayout that I only show while the animation is happening.

    I get the starting position of my burried button like this (it's funny to see these C style coding mechanisms in Java, they're pretty rare these days:

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

    So, now I have my starting point.

    My end point is an absolute coordinate on the screen, you can assign that however you wish

    Then I make a little Animations helper class that lets me pass in all the coordinates, the callback, and the duration of the 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;
        }
    }
    

    and we need a listener to let us know when the animation is done to clear it

     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 to delete the ImageView and hide the animation Layout until we need it again.
                clearAnimation();       
            }
        };
    

    And lastly we throw it all together and press GO

    int fromLoc[] = new int[2];
        v.getLocationOnScreen(fromLoc);     
        float startX = fromLoc[0];
        float startY = fromLoc[1];      
        RelativeLayout rl = ((RelativeLayout)findViewById(R.id.sticker_animation_layout));
        ImageView sticker = new ImageView(this);
    
        int stickerId = getStickerIdFromButton(v);
        if(stickerId == 0){
            stickerAnimationPlaying = false;
            return;         
        }
    
        float destX = 200.0f;//arbitrary place on screen
        float destY = 200.0f;//arbitrary place on screen
    
        sticker.setBackgroundResource(stickerId);
        sticker.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
        rl.addView(sticker);
        Animations anim = new Animations();
        Animation a = anim.fromAtoB(startX, startY, destX, destY, animL,750);
        sticker.setAnimation(a);
        a.startNow();
    

提交回复
热议问题