I have this code:
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator animX = ObjectAnimator.ofFloat(button3, "X", 0f, 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat(button3, "Y", 0f, 50f);
AnimatorSet animSet = new AnimatorSet();
animSet.playSequentially(animX, animY);
animSet.setDuration(500);
animSet.start();
}
});
I’m trying to move "button3" with ObjectAnimator randomly across the screen for a set amount of time, but I can’t figure out how to multiply the X and Y values to make the button position random.
2
Answers
You can do like this. In my example, I showed that the button moves only inside its container(id=parentView) and does not go beyond it. I limit the width and height, you can change the value you want(or match_parent)
MainActivity
activity_main.xml