punksta
punksta

Reputation: 2808

android animation move view to another view

I have two views in different layouts I want to move one to another. What's wrong with my code? Y animation plays wrong. First view is located in fragment's layout, second in status bar

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

Upvotes: 10

Views: 9076

Answers (2)

Sachin Singh
Sachin Singh

Reputation: 169

Try this

private fun moveView(viewToBeMoved: View, targetView: View) {
    val targetX: Float =
        targetView.x + targetView.width / 2 - viewToBeMoved.width / 2
    val targetY: Float =
        targetView.y + targetView.height / 2 - viewToBeMoved.height / 2

    viewToBeMoved.animate()
        .x(targetX)
        .y(targetY)
        .setDuration(2000)
        .withEndAction {
            targetView.visibility = View.GONE
        }
        .start()
}

Upvotes: 1

mouayad
mouayad

Reputation: 443

I have a another solution for you:(move viewOne to 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); 

Upvotes: 28

Related Questions