I'm working with a custom ScrollPane, but I can't find how to change the velocity of scrolling (not on fling action). Can anybody tell me how reduce scrolling velocity on calling setScrollY() or setScrollX()?
-
\$\begingroup\$ By "not on fling action", do you mean that "setVelocityX" & "setVelocityY" don't work for your use case ? I don't get it. \$\endgroup\$– KhopaCommented Apr 8, 2015 at 13:40
-
\$\begingroup\$ After calling setScrollY my ScrollPane has been scrolling too hast to that position \$\endgroup\$– SlampyCommented Apr 8, 2015 at 13:43
2 Answers
I had a look at the ScrollPane source code on GitHub. That's quite a difficult problem.
The animation is done in the ScrollPane's act method. If you set the scroll value manually, the visual scroll value used for rendering will align on the scroll value over time through an interpolation process (usually in a few frame).
Here is the code responsible for this behaviour in the ScrollPane's act method :
// ...
if (visualAmountX != amountX) {
if (visualAmountX < amountX)
visualScrollX(Math.min(amountX, visualAmountX + Math.max(200 * delta, (amountX - visualAmountX) * 7 * delta)));
else
visualScrollX(Math.max(amountX, visualAmountX - Math.max(200 * delta, (visualAmountX - amountX) * 7 * delta)));
animating = true;
}
if (visualAmountY != amountY) {
if (visualAmountY < amountY)
visualScrollY(Math.min(amountY, visualAmountY + Math.max(200 * delta, (amountY - visualAmountY) * 7 * delta)));
else
visualScrollY(Math.max(amountY, visualAmountY - Math.max(200 * delta, (visualAmountY - amountY) * 7 * delta)));
animating = true;
}
// ...
As you can see, the speed depends mostly on the distance between the current visual value and the setted "official" value. Some values that limit and defines the speed are hardcoded ("200", "7"), so there is currently no way to setup the speed for this animation cleanly.
Here are some ideas/solution :
Override the act method of the Scrollpane. Just copy the source from the full act method and tune the hardcoded values (but that's dirty)
Create customs actions to update the visual scroll value and the scroll value manually.
Modify the Scrollpane class to make the animation speed use the velocity attributes and make a pull request on the libgdx Github repository, so that everybody will maybe benefit of this fix one day.
-
\$\begingroup\$ What about make copy all libgdx ScrollPane class, rename it and extend it and change (200, 7) values? :) \$\endgroup\$– SlampyCommented Apr 8, 2015 at 14:54
-
\$\begingroup\$ You don't need to override the whole class, just the act method should be enough. But, that's a very dirty quick fix/hack. \$\endgroup\$– KhopaCommented Apr 8, 2015 at 14:57
-
\$\begingroup\$ Thks, man)) Your answer was very helpfull!!!)) \$\endgroup\$– SlampyCommented Apr 8, 2015 at 15:02
-
\$\begingroup\$ Thanks for that little research on source code. And here is how I solved that problem, one dirty trick - call ScrollPane#act(1f) right after you set scroll position. This may break some action logic for descendant actors, but in general case it could work. \$\endgroup\$ Commented Mar 7, 2016 at 7:53
You could try creating your own version of the ScrollPane
by inheriting from ScrollPane
and then overriding visualScrollX(float pixelsX)
and visualScrollY(float pixelsY)
, and multiply the parameter by some factor before calling the base class implementation;
class MyScrollPane extends ScrollPane {
@Override
protected void visualScrollX(float pixelsX) {
super.visualScrollX(pixelsX * 0.5f);
}
@Override
protected void visualScrollY(float pixelsY) {
super.visualScrollY(pixelsY * 2.0f);
}
}
-
\$\begingroup\$ Thaks for answer, but this approarch don't working. Do you sure about overriding that methods? \$\endgroup\$– SlampyCommented Apr 8, 2015 at 13:52
-
\$\begingroup\$ I trace it up. You are right - this methods are working with scrolling coordinates. But i can't count factor(( \$\endgroup\$– SlampyCommented Apr 8, 2015 at 14:08
-
\$\begingroup\$ From your initial question I thought you meant the scroll speed achieved when scrolling with your finger, are you instead talking about explicitly setting the scroll position using
setScrollY
and the velocity that use to get to the target position? Can you elaborate on what you want to achieve and how you're currently using theScrollPane
? \$\endgroup\$ Commented Apr 8, 2015 at 14:14 -
\$\begingroup\$ Oh, sorry for that! My target is to reduce scrolling speed when calling setScrollY(). Without finger \$\endgroup\$– SlampyCommented Apr 8, 2015 at 14:20