Backboard
Backboard copied to clipboard
add animation listener
When I create an animation using actor like the following:
new Actor.Builder(SpringSystem.create(), rootView.findViewById(R.id.circle)) .addTranslateMotion(Imitator.TRACK_DELTA, Imitator.FOLLOW_EXACT, MotionProperty.X).build();
I want to add a listener to control the behavior of the animation itself or to track the user actions, ex. when the user touch the view down, up or start moving it.
Note: I want to use this animation to create "Slide to Cancel" function. how can I achieve this function using the above animation.
You can track the user actions by adding a View.OnTouchListener
:
new Actor.Builder(SpringSystem.create(), rootView.findViewById(R.id.circle))
.addTranslateMotion(Imitator.TRACK_DELTA, Imitator.FOLLOW_EXACT, MotionProperty.X)
.onTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// custom behavior
return true;
}
})
But if you want to actually add new behavior to the animation, you can do something like this:, which will fade the view as the user moves it:
final View circle = rootView.findViewById(R.id.circle);
SpringSystem springSystem = SpringSystem.create();
new Actor.Builder(springSystem, rootView.findViewById(R.id.circle))
.addMotion(springSystem.createSpring(), Imitator.TRACK_DELTA, Imitator.FOLLOW_EXACT,
0, MotionProperty.X, new SimpleSpringListener() {
@Override
public void onSpringUpdate(Spring spring) {
circle.setAlpha(
1 - 2 * Math.abs((float) spring.getCurrentValue())
/ rootView.getMeasuredWidth());
}
})
.build();
But if you're looking to control the animation, that may get a little complicated since the spring will animate on its own, so you will need to decide whether or not to use the spring's value or your own custom value.
That's program Errors create your
Actor walk = new Actor.Builder(SpringSystem.create(), walker) .addMotion( new MotionImitator(MotionProperty.X), new Performer(View.TRANSLATION_X)) .build();
rootView.findViewById(R.id.circle))