title: Android reducing height of an image leaves trail
body: <p>I'm trying to set the height of an image view using single touch for my app. I want it to decrease image height as I swipe up and increase as I swipe down. This is my code:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private static final String DEBUG_TAG = "MainActivity";
      private VelocityTracker mVelocityTracker = null;
      LinearLayout gameBoardView;
  
      /**
       * Called when the activity is first created.
       */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          gameBoardView = (LinearLayout) findViewById(R.id.gameBoard);
          initCardFromTop();
      }
  
      private void initCardFromTop() {
          Log.e("MainActivity: initCardFromTop()", "called");
          final ImageView ivTopCard = new ImageView(MainActivity.this);
          ivTopCard.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                  LinearLayout.LayoutParams.MATCH_PARENT));
          ivTopCard.setImageResource(R.drawable.up_arrow);
          ivTopCard.setBackgroundColor(getResources().getColor(R.color.emerald));
  //        ivTopCard.setScaleType(ImageView.ScaleType.FIT_CENTER);
  //        ivTopCard.setAdjustViewBounds(true);
  //        ivTopCard.setBackgroundResource(R.drawable.up_arrow);
          ivTopCard.setOnTouchListener(new ImageTouchListener());
          gameBoardView.addView(ivTopCard);
      }
  
      private void setHeight(ImageView ivCard, int height) {
          height = (height &lt; 0) ? 0 : height;
          ViewGroup.LayoutParams params = ivCard.getLayoutParams();
          params.height = height;
          ivCard.setLayoutParams(params);
      }
  
      private class ImageTouchListener implements View.OnTouchListener {
          @Override
          public boolean onTouch(View view, MotionEvent event) {
              int index = event.getActionIndex();
              int action = event.getActionMasked();
              int pointerId = event.getPointerId(index);
  
              switch (action) {
                  case MotionEvent.ACTION_DOWN:
                      if (mVelocityTracker == null) {
                          // Retrieve a new VelocityTracker object to watch the velocity of a motion.
                          mVelocityTracker = VelocityTracker.obtain();
                      } else {
                          // Reset the velocity tracker back to its initial state.
                          mVelocityTracker.clear();
                      }
                      // Add a user's movement to the tracker.
                      mVelocityTracker.addMovement(event);
                      break;
                  case MotionEvent.ACTION_MOVE:
                      mVelocityTracker.addMovement(event);
                      // When you want to determine the velocity, call
                      // computeCurrentVelocity(). Then call getXVelocity()
                      // and getYVelocity() to retrieve the velocity for each pointer ID.
                      mVelocityTracker.computeCurrentVelocity(100);
                      float deltaHeight = VelocityTrackerCompat.getYVelocity(mVelocityTracker,
                              pointerId);
                      setHeight((ImageView) view, (int) (view.getHeight() + deltaHeight));
                      break;
                  case MotionEvent.ACTION_UP:
                  case MotionEvent.ACTION_CANCEL:
                      // Return a VelocityTracker object back to be re-used by others.
                      mVelocityTracker.recycle();
                      break;
              }
              return true;
          }
      }
  }
  </code></pre>
  
  <p>It works absolutely fine when I swipe up and increase height but when I try to reduce the height then it leaves a long trail which I don't want. It looks like this:
  <img src="https://i.stack.imgur.com/KTCwX.png" alt="enter image description here"></p>
  
  <p>Interesting thing is that it works fine when I remove the background color of image and only use source but I need the background and using another imageview to do the same isn't a good option IMO. I'm pretty sure that I'm missing something very basic here. Please help, I'm stuck for very long now.</p>