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));
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()
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With