title: How to smoothly animate objects in Android
body: <p>I have a view object in Android and I would like to animate it such that it flows from right to left on the screen. I currently use <code>ObjectAnimator</code> with the following code:</p>
  <pre><code>ObjectAnimator animation = ObjectAnimator.ofFloat(view, &quot;translationX&quot;, (-1)* 50* currentTimeSlot);
  animation.setDuration(90);
  animation.start();
  </code></pre>
  <p>The counting variable <code>currentTimeSlot</code> is just incremented after 100 Milliseconds by using <code>handler.postDelayed</code>.</p>
  <p>Actually the code works and does what it should. However, the animation is not really smooth and I would like to know how I can make the animation smooth? Do you have any idea?</p>
  <p><strong>Update</strong>: Here is the whole code with the Fragment and the custom view that should be smoothly animated:</p>
  <pre><code>public class Test extends Fragment {
  
      private Handler handler = new Handler();
      public static final int DELAY_IN_MILLIS = 100;
      private int currentTimeLeft_MILLIS;
      int helpCounterRun =0;
      private boolean viewHasBeenCreated = false;
      private FragmentTestBinding binding;
      View view;
      Drawable dr;
      int currentTimeSlot;
  
      private ConstraintLayout constraintLayout;
  
      public Test() {
  
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          currentTimeLeft_MILLIS = 90 * 1000;
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          binding = FragmentTestBinding.inflate(inflater, container, false);
          viewHasBeenCreated = true;
          getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  
          container.getContext();
          constraintLayout= binding.constraintLayout;
          view = new View(getActivity());
  
          dr = ContextCompat.getDrawable(getActivity(),R.drawable.light_bulb_layer_list);
          currentTimeSlot =0;
          countDownTime();
          return binding.getRoot();
      }
  
      private void updateScreen() {
          binding.textViewTimeLeftValue.setText(&quot;&quot; + currentTimeLeft_MILLIS /1000);
  
          //Set the parameter and background of the view
          constraintLayout.removeView(view);
          view.setLayoutParams(new ViewGroup.LayoutParams(0, 0));
          view.setBackground(dr);
  
          //Shift view to the right at the beginning of the level
          if (currentTimeSlot ==0) {
              view.setTranslationX(view.getWidth());
          }
  
          // Shift the view 70 pixels to the left in each iteration
          ObjectAnimator animation = ObjectAnimator.ofFloat(view, &quot;translationX&quot;, (-1)* 70* currentTimeSlot);
          animation.setDuration((long) (DELAY_IN_MILLIS*0.9));
          animation.start();
  
  
          // Set the ConstraintLayout programatically for the view
          view.setId(View.generateViewId());
          ConstraintLayout constraintLayout = binding.constraintLayout;
          ConstraintSet constraintSet = new ConstraintSet();
          constraintLayout.addView(view);
          constraintSet.clone(constraintLayout);
          constraintSet.constrainPercentHeight(view.getId(), 0.071f);
          float widthConstrainPercentage_element1 = 0.25f;
          constraintSet.constrainPercentWidth(view.getId(), widthConstrainPercentage_element1);
          constraintSet.connect(view.getId(),ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM,0);
          constraintSet.connect(view.getId(),ConstraintSet.TOP,ConstraintSet.PARENT_ID ,ConstraintSet.TOP,0);
          constraintSet.connect(view.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID ,ConstraintSet.LEFT,0);
          constraintSet.connect(view.getId(),ConstraintSet.RIGHT,ConstraintSet.PARENT_ID ,ConstraintSet.RIGHT,0);
  
          constraintSet.setHorizontalBias(view.getId(),  1.0f);
          constraintSet.setVerticalBias(view.getId(), 0.049f);
          constraintSet.applyTo(constraintLayout);
  
      }
  
      private void countDownTime(){
          currentTimeLeft_MILLIS = currentTimeLeft_MILLIS - DELAY_IN_MILLIS;
          currentTimeSlot++;
  
          handler.postDelayed(new Runnable() {
              @Override
              public void run() {
                  helpCounterRun++;
                  if(viewHasBeenCreated) {
                      countDownTime();
                  }
              }
  
          }, DELAY_IN_MILLIS);
          updateScreen();
      }
  
      @Override
      public void onDestroyView() {
          super.onDestroyView();
          viewHasBeenCreated = false;
          handler.removeCallbacksAndMessages(null);
      }
  
  }
  </code></pre>
  <p><strong>Reminder</strong>: Does nobody have any idea how to animate objects in Android in a good way (such that the animation is smooth)? I'll appreciate every comment.</p>