A
A
Andrew2020-08-07 18:37:59
Java
Andrew, 2020-08-07 18:37:59

How to force an object to return to its original position java android studio?

There is an ImaveView object with the following parameters:

<ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ball"
        android:layout_marginBottom="80dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:id="@+id/ball"
        />


When pressed, the object moves up:
ball.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                //Toast.makeText(getApplicationContext(), "Ударил по мячу", Toast.LENGTH_SHORT).show();
                ObjectAnimator animation = ObjectAnimator.ofFloat(v, "y", 20f);
                animation.setDuration(500);
                animation.start();
}
});


How to return an object to its original position after it has reached a point?
What I googled :)
I read the parameters after setContentView, then set them:
final ConstraintLayout.MarginLayoutParams params = (ConstraintLayout.MarginLayoutParams) ball.getLayoutParams();

And then with a delay I try to set the initial parameters.

ball.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                //Toast.makeText(getApplicationContext(), "Ударил по мячу", Toast.LENGTH_SHORT).show();
                ObjectAnimator animation = ObjectAnimator.ofFloat(v, "y", 20f);
                animation.setDuration(500);
                animation.start();


                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        params.setMargins(0,0,50,80);
                        ball.setLayoutParams(params);
                    }
                }, 3000);
}
});


I think it's the layout_constraintBottom_toBottomOf, I changed it to the id of another object.
Tell me where to dig to return the object to its original position?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-08-08
@prolisk

Don't animate y, animate translationY and reset it to 0 when needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question