android动画移动视图到另一个视图

时间:2014-12-02 13:22:51

标签: android android-layout coordinates android-animation

我在不同的布局中有两个视图,我想将它们移动到另一个。我的代码出了什么问题? Y动画播放错误。第一个视图位于片段的布局中,第二个位于状态栏

    ...
    int p1[] = new int[2];
    int p2[] = new int[2];
    viewOne.getLocationInWindow(p1);
    viewTwo.getLocationInWindow(p2);


    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet
            .play(ObjectAnimator.ofFloat(expandedImageView, "X", p1[0], p2[0] - p1[0]))
            .with(ObjectAnimator.ofFloat(expandedImageView, "Y", p1[1], p2[1] - p1[1]))
            .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScale))
            .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScale));

1 个答案:

答案 0 :(得分:22)

我还有另一个解决方案:(将viewOne移动到viewTwo)

 TranslateAnimation animation = new TranslateAnimation(0, viewTwo.getX()-viewOne.getX(),0 , viewTwo.getY()-viewOne.getY());
    animation.setRepeatMode(0);
    animation.setDuration(3000);
    animation.setFillAfter(true);
    viewOne.startAnimation(animation); 
相关问题