Picture copyright Aurelien Ribon
The Universal Tween Engine allows you to create smooth interpolations on every attribute from every object in your project !
I would like to thank the author of the official version, Aurelien Ribon, a lot for helping me porting the project.
You can take a look at an example here:
Universal Tween Engine demo
or Android Application:
https://play.google.com/store/apps/details?id=aurelienribon.tweenengine.demo
And amazing sample made with b4a?
Check out post 6 by Sorex!
I ported almost all functions from this engine to Basic4Android.
So you can make simple tweens with callbacks to powerful animation sequences.
Examples to make with this library:
- Games: Imagine Jewelquest where the sprites have to move from the game (with an elastic animation, bounce, etc)
- Livewallpaper: imagine what you can do
- Menu's
- Intro's
- And a lot more!
I will try to explain this library as good as possible.
There are 6 classes available to use in the TweenEngine:
B4X:
Dim Tween As Tween
Dim Manager As TweenManager
Dim Constants As TweenConstants
Dim Callback As TweenCallback
Dim Path1 As TweenPath
Dim TimeLine As TweenTimeline
A Tween is an animation of an object, view, ...
Currently you can animate a Label, ImageView and Panel. (Due B4A uses wrappers for the views, we needed to create seperate methods for every view. We also couldn't cast from an Object to a viewwrapper.) But don't worry! You can still add your view to animate to a panel and tween the panel.
Let's go over a sample of animating a view.
B4X:
'Load Activity stuff
Activity.LoadLayout("Main.bal")
Activity.AddMenuItem("Start Tween", "mnuTween")
'Initialize the TweenManager
Manager.Initialize
'Initialize the tween
Tween1.Initialize("Tween")
'A callback is used when you want to monitor a tween like tween completion, etc
Callback.Initialize("TweenCallback")
'We will use a timer to update our animation.
'Normally, we should use a thread to remain maximum fps.
tmrUpdate.Initialize("TweenUpdate", 10)
'Next, we register a Tween Accessor if the object you will use is a panel.
'If the tween you will use is an imageview, you'll use registerImageAccessor, etc
Tween1.registerPanelAccessor
'Let's create our tween.
'First we need to call one of the 4 'initializers'. These will construct your tweens otherwise you'll receive a nullpointer.
'These are: - To, From, Set and Call - they are explained in the methods in the IDE.
'Our first parameter is the object/view we will add.
'Second param is a tweenConstants. These are X, Y or XY
'Last parameter is our duration in secs or ms.
'This is the unit you want, the engine doesn't care
'However, if you choose seconds for instance, you need to use the same unit in every call to the engine.
'That means that the deltas in the manager.update() method will need to be in seconds too.
'As long as you use the same unit everywhere, you can use the unit you want.
Tween1.PanelTo(pnlTween, Constants.POSITION_XY, 30)
'Then we set the target's x and y position to go to.
Tween1.setTarget(105,200)
'Next we create an ease from the constants. A dozen are available to use.
Tween1.Ease(Constants.ELASTIC_IN)
'Sets a listener before you set the callback!
Callback.Listener = True
'A callback is used when you can to know what happened with the Tween. OnEvent is called with the TweenCallback eventname you initialized before
Tween1.Callback = Callback.TweenCallback
'Adds the tween to the TweenManager.
'Calling TweenManager.start will start all tweens associated with it.
Manager.addTween(Tween1(0).Tween)
Manager.addTween(Tween1(1).Tween)
Sub mnuTween_Click
'Starts the tween and enables the timer.
Manager.StartTween
tmrUpdate.Enabled = True
End Sub
Sub TweenUpdate_Tick
'Updates the tween.
Manager.Update(0.1)
End Sub
Sub TweenCallback_OnEvent (Event As Int)
Log("Event: " & Event)
End Sub
A lot of other methods are available to the tweens like resuming, pausing, getting information, repeating, delays, waypoints, etc.
A sample for TweenTimelines isn't available yet, but you can find more information here:
java-universal-tween-engine - The Universal Tween Engine allows you to create smooth interpolations on every attribute from every object in your project ! - Google Project Hosting
TweenConstants are the constants used for the Tweens.
All constants (except 3 Position_x, y and xy) are used for the easing of the tween.
TweenPath isn't really tested.
It has 1 method:
B4X:
/**
* Computes the next value of the interpolation, based on its waypoints and
* the current progress.
*
* @param t The progress of the interpolation, between 0 and 1. May be out
* of these bounds if the easing equation involves some kind of rebounds.
* @param points The waypoints of the tween, from start to target values.
* @param pointsCnt The number of valid points in the array.
* @return The next value of the interpolation.
*/
TweenCallback is the callback from your methods. OnEvent is called with the event as an int.
You can use this to start new tweens, or to open an activity when a tween ended.
It would also be great if people could make nice examples to share with other users. The intro of the Android app is available on the universal Tween Engine google code: / - java-universal-tween-engine - The Universal Tween Engine allows you to create smooth interpolations on every attribute from every object in your project ! - Google Project Hosting
As last: the credits.
The Universal Tween Engine is licensed under Apache License 2.0.
This means you'll have to credit the author(Aurelien Ribon) and a link to their website (Aurelien Ribon's Dev Blog)
Users are free to use the lib in any projects, commercial or not. If you distribute a modified version of the library, you need to retain the original attribution.
In every licensed file, any attribution notices in redistributed code must be preserved; and, in every licensed file changed, a notification must be added stating that changes have been made to that file.
Javadocs are available here:
Universal Tween Engine API
If you don't know what a function does, or arguments, visit that link!
The javadocs are extremely good documented and if you would like a new feature from the Javadocs, reply on this topic. Do not pm me about this.
I would also like to thank Sorex for beta testing and the usual bunch on the chat: tds, NJDude, HotShoe, Barx.
(feel free to join us and talk about b4a! We are there 24/7
Basic4Android Chat
In the attachments, you can find the usual stuff. A sample, lib files and readme
If you have any questions, please ask them in this topic.
Have fun!
Tomas
Note 1. It is best to associate all tweens with a manager instead of calling them manually.
Note 2. You can not call methods like to(...).delay(...).repeat(...).start becaus e Basic4Android handles only known types.
note 3. You can also declare an array of tweens like:
B4X:
dim tween(2) as Tween
tween(0).do
tween(1).do
note 4. If you receive some kind of error: "No TweenAccessor found or to this object." means that your accessor is incorrect. Probably, you will have called the wrong Accessor (registerPanelAccessor, etc) to the wrong type (e.g imageview)
Attachments
Last edited: