ValueAnimator is fun. Specially when you want to add some fancy animation in android app. Some simple example code is written below.
ValueAnimator example 1:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private float ballXpos;
private float ballYpos;
private int screenHeight;
private int screenWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
screenHeight = displayMetrics.heightPixels;
screenWidth = displayMetrics.widthPixels;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ballXpos = binding.ball.getX();
ballYpos = binding.ball.getY();
ValueAnimator valueAnimatorY = ValueAnimator.ofFloat(ballYpos, screenHeight - binding.ball.getHeight() * 3);
valueAnimatorY.setDuration(1000);
valueAnimatorY.setInterpolator(new DecelerateInterpolator());
valueAnimatorY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
binding.ball.setY((Float) valueAnimator.getAnimatedValue());
}
});
ValueAnimator valueAnimatorX = ValueAnimator.ofFloat(ballXpos, screenWidth - binding.ball.getHeight() * 2);
valueAnimatorX.setDuration(1000);
valueAnimatorX.setInterpolator(new AccelerateInterpolator());
valueAnimatorX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
binding.ball.setX((Float) valueAnimator.getAnimatedValue());
}
});
valueAnimatorX.start();
valueAnimatorY.start();
}
}, 1000);
}
}
Result:
ValueAnimator example 2:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private float ballXpos;
private float ballYpos;
private int screenHeight;
private int screenWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
screenHeight = displayMetrics.heightPixels;
screenWidth = displayMetrics.widthPixels;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ballXpos = binding.ball.getX();
ballYpos = binding.ball.getY();
ValueAnimator valueAnimatorY = ValueAnimator.ofFloat(ballYpos, screenHeight - binding.ball.getHeight() * 3);
valueAnimatorY.setDuration(1000);
valueAnimatorY.setInterpolator(new AccelerateInterpolator());
valueAnimatorY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
binding.ball.setY((Float) valueAnimator.getAnimatedValue());
}
});
ValueAnimator valueAnimatorX = ValueAnimator.ofFloat(ballXpos, screenWidth - binding.ball.getHeight() * 2);
valueAnimatorX.setDuration(1000);
valueAnimatorX.setInterpolator(new DecelerateInterpolator());
valueAnimatorX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
binding.ball.setX((Float) valueAnimator.getAnimatedValue());
}
});
valueAnimatorX.start();
valueAnimatorY.start();
}
}, 1000);
}
}