什么是动画从一个视图坐标到另一个正确的方法是什么? [英] What's the correct way to animate a View from one coordinate to another?

查看:137
本文介绍了什么是动画从一个视图坐标到另一个正确的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做到以下几点。我有一组对他们的一些图标按钮。当用户点击一个,我想向大家介绍一个新视图开始在同一个坐标为挖掘图标,然后新的视图会在转移到其他位置在屏幕上,当它到达那里被去除。

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 coordinate as the tapped icon, and then that new View would shift over to some other location on the screen and when it arrived there be removed.

我知道如何创建一个新的观点,并添加/删除它的父RelativeLayout的(如果它不是一个RelativeLayout的?)和所有。什么我不清楚是如何得到被窃听按钮的绝对坐标(因为它里面父布局只是一个元素,在另一个父布局),然后将其坐标和应用动画,然后什么通知我,它已经到了是去哪儿,这样我可以将其删除?

I know how to create a new view and add/remove it to the parent RelativeLayout (should it not be a RelativeLayout?) and all that. What I'm not clear on is how to get the absolute coordinates of the button that was tapped (since it's just one element inside a parent layout, inside another parent layout) and then set its coordinates and apply an animation, and then what would notify me that it has arrived where it was going, so that I can remove it?

无法找到如何反正做到这一点的例子,所以,希望有人能在正确的方向指向我。

Can't find an example of how to do this anyway, so, hoping someone can point me in the right direction.

推荐答案

所以,事实证明,这是更简单的比我想象。

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

我创建了一个全屏幕RelativeLayout的,而动画正在发生的事情,我只显示。

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

我得到这样我埋按钮的起始位置(这很有趣地看到在Java中使用这些C风格的编码机制,他们是pretty的罕见的,这些天:

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

和最后我们把它全部一起,preSS GO

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

这篇关于什么是动画从一个视图坐标到另一个正确的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆