Answer the question
In order to leave comments, you need to log in
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"
/>
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();
}
});
final ConstraintLayout.MarginLayoutParams params = (ConstraintLayout.MarginLayoutParams) ball.getLayoutParams();
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);
}
});
Answer the question
In order to leave comments, you need to log in
Don't animate y, animate translationY and reset it to 0 when needed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question