Brief introduction
Official website
GitHub
rebound is a spring animation library produced by Facebook with a pop animation library for the corresponding iOS version, which is also very powerful. Facebook is really an example of Internet enterprises, open source a lot of practical open Source Library, great!!!
Before explaining rebound , take a look at our Sina Weibo pop-up menu based on rebound high imitation, see:
Anyway, why did Facebook launch this library? The main is the existing animation from the real physical world difference is more obvious, in order to let the animation look real natural, with the effect of power,rebound came into being. Two more important parameters, one is tensile tension, one is the friction friction, the greater the tension, the more obvious the spring effect; the greater the friction, the greater the resistance and the less obvious the frame effect. If the value of this friction is set to 0, just like the real world in a vacuum, a little friction is not, this spring effect will always be unlimited repetition, can not stop, do not believe to see:
Let's change the value of friction to 1 try:
Primary usage
rebound provides a very concise API method for us to call, we just need to configure some simple parameters to use, the following look at the example of the official website:
// Create a system to run the physics loop for a set of springs.SpringSystem springSystem = SpringSystem.create();// Add a spring to the system.Spring spring = springSystem.createSpring();// Add a listener to observe the motion of the spring.spring.addListener(new SimpleSpringListener() { @Override public void onSpringUpdate(Spring spring) { // You can observe the updates in the spring // state by asking its current value in onSpringUpdate. float value = (float) spring.getCurrentValue(); float scale = 1f - (value * 0.5f); myView.setScaleX(scale); myView.setScaleY(scale); }});// Set the spring in motion; moving from 0 to 1spring.setEndValue(1);
Let's look at the corresponding:
You see, it seems that the spring effect is not obvious,rebound the default tensile and frictional parameters are 40 and 7 respectively, we look at the rebound inside there is a defaultconfig
public static SpringConfig defaultConfig = SpringConfig.fromOrigamiTensionAndFriction(40, 7);
In order to make the spring effect more obvious, we modify the value of the next springconfig , the code is as follows:
spring.setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(100,1));
We change the tensile value to 100 and the friction value to 1, as follows:
The effect is great!
Advanced usage: Multiple view chain animations
What if you want to do a lot of view chain animations? The rebound also provides the Springchain interface. Look directly at the code:
SpringChain springChain = SpringChain.create(40,6,50,7);int childCount = viewGroup.getChildCount();for (int i = 0; i < childCount; i++) { final View view = viewGroup.getChildAt(i); springChain.addSpring(new SimpleSpringListener() { @Override public void onSpringUpdate(Spring spring) { view.setTranslationY((float) spring.getCurrentValue()); } });}List<Spring> springs = springChain.getAllSprings();for (int i = 0; i < springs.size(); i++) { springs.get(i).setCurrentValue(400);}springChain.setControlSpringIndex(2).getControlSpring().setEndValue(0);
As follows:
Let's take a look at the Springchain class and create it with two creation methods:
- Default no parameter Create ()
- Create with parameters (int maintension,int mainfriction,int attachmenttension,int attachmentfriction)
Where the first parameter with the parameter represents the pull factor of the dominant spring, the second parameter represents the friction coefficient of the dominant spring, and the third and fourth represent the attached tensile and frictional coefficients
Springchain needs to set up a spring with a dominant control function, which is set by the Setcontrolspringindex method.
High imitation Sina pop-up menu
First look at the high imitation after:
Here is an example code:
Popmenu Popmenu = new Popmenu.builder (). attachtoactivity (Mainactivity.this). AddMenuItem (New Popmenuitem (" Word ", getresources (). getdrawable (R.drawable.tabbar_compose_idea)). AddMenuItem (New Popmenuitem (" Photo/video ", GetR Esources (). getdrawable (R.drawable.tabbar_compose_photo)). AddMenuItem (New Popmenuitem ("headline article", Getresources (). Getdrawable (R.drawable.tabbar_compose_headlines)). AddMenuItem (New Popmenuitem ("Sign In", Getresources (). Get Drawable (r.drawable.tabbar_compose_lbs)). AddMenuItem (New Popmenuitem ("Reviews", Getresources (). Getdrawable (R.D Rawable.tabbar_compose_review)). AddMenuItem (New Popmenuitem ("More", Getresources (). Getdrawable (r.drawable.t Abbar_compose_more)). Setonitemclicklistener (New Popmenuitemlistener () {@Override public void Onitemclick (Popmenu popmenu, int position) {Toast.maketext (mainactivity . This, "You clicked on the" + Position + "position", Toast.length_short). Show (); }}). Build (); Popmenu.show ();
Here because of the length of reason, not to explain the implementation of the principle, such as the need to see the source to my GitHub download, as: High imitation Sina pop-up menu
Rebound-android's Spring Animation library