0

I've added the drag functionality to a component using ViewDragHelper and I'm trying to add a fade animation to the component so that when the user touches the screen and drag it, it changes the component's alpha using an animation. The problem is that the animation waits for the user's interaction to stop and then it applies the animation so that during the component transition, you can't see the alpha animation,

override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int {
        if(top < verticalRange)
            return verticalRange
        backgroundView.animate().alpha((backgroundView.alpha - 0.05).toFloat()).duration = 10
        return top
    }

The on touch Event:

 override fun onTouchEvent(event: MotionEvent): Boolean {
            return if (isDragEnabled && (isControlScreenLayoutTarget(event) || isMoving)) {
                viewDragHelper.processTouchEvent(event)
                true
            } else {
                super.onTouchEvent(event)
            }
        }
Foad Saeidi Nik
  • 260
  • 1
  • 5
  • 20
  • You should add the codes of `onTouchEvent` to make it clearer. – Duy Khanh Nguyen Oct 28 '19 at 02:27
  • @DuyKhanhNguyen I've edited the question and added the onTouchEvent – Foad Saeidi Nik Oct 28 '19 at 02:32
  • There are no connections between your blocks of code, we can't know how/where do you call `clampViewPositionVertical`. You should add enough codes that should the flow. – Duy Khanh Nguyen Oct 28 '19 at 02:37
  • @DuyKhanhNguyen I will add more code if you need it, but the bug doesn't have anything to do with the drag flow. I added some logs to clampViewPositionVertical function and I know that the function is called, but the alpha animation doesn't work until the user touch/ interaction stops. – Foad Saeidi Nik Oct 28 '19 at 02:45

1 Answers1

0

You should not start your animation in clampViewPositionVertical(). That method is called to return the boundaries where the view will move, actually scroll.

Instead you should wait until view is released, in other words, start your animation as soon as the range check is fulfilled in the onViewReleased().

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148