将视图从一个坐标设置为另一个坐标的正确方法是什么?

时间:2012-03-09 00:32:36

标签: android animation

我想做以下事情。我有一组按钮,上面有一些图标。当用户点击一个时,我想引入一个新的视图,该视图以与点击的图标相同的坐标开始,然后新的视图将转移到屏幕上的其他位置,当它到达时将被删除。

我知道如何创建一个新视图并将其添加/删除到父RelativeLayout(如果它不是RelativeLayout?)以及所有这些。我不清楚的是如何获取被点击的按钮的绝对坐标(因为它只是父布局中的一个元素,在另一个父布局中)然后设置其坐标并应用动画,然后会是什么通知我,它已经到达了它的位置,以便我可以删除它?

无论如何都找不到如何做到这一点的例子,所以,希望有人可以指出我正确的方向。

2 个答案:

答案 0 :(得分:12)

所以,事实证明这比我想象的要简单得多。

我创建了一个全屏的RelativeLayout,我只在动画发生时显示。

我得到了这样的隐藏按钮的起始位置(在Java中看到这些C风格的编码机制很有趣,这些日子非常罕见:

int fromLoc[] = new int[2];
v.getLocationOnScreen(fromLoc);     
float startX = fromLoc[0];
float startY = fromLoc[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;
    }
}

我们需要一个监听器让我们知道动画何时清除它

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

最后我们将它们全部放在一起然后按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();

答案 1 :(得分:1)

我只是在框架布局上添加我对中心位置的视图,并将我的视图转换为x轴和y轴。请尝试以下代码: -

在framelayout上添加图片视图

    imgHeart = new ImageView(getBaseContext());
    imgHeart.setId(R.id.heartImage);
    imgHeart.setImageResource(R.drawable.material_heart_fill_icon);
    imgHeart.setLayoutParams(new FrameLayout.LayoutParams(50, 50, Gravity.CENTER));
    mainFrameLaout.addView(imgHeart);

在图像视图中添加动画

       imgHeart.animate()
            .scaleXBy(6)
            .scaleYBy(6)
            .setDuration(700)
            .alpha(2)
            .setListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    imgHeart.animate()
                            .scaleXBy(-6f).scaleYBy(-6f)
                            .alpha(.1f)
                            .translationX((heigthAndWidth[0] / 2) - minusWidth)
                            .translationY(-((heigthAndWidth[1] / 2) - minusHeight))
                            .setDuration(1000)
                            .setListener(new Animator.AnimatorListener() {
                                @Override
                                public void onAnimationStart(Animator animation) {
                                }

                                @Override
                                public void onAnimationEnd(Animator animation) {
                                // remove image view from framlayout
                                }
                                @Override
                                public void onAnimationCancel(Animator animation) {
                                }

                                @Override
                                public void onAnimationRepeat(Animator animation) {
                                }
                            }).start();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            }).start();
相关问题