B4A Library [Library] Universal Tween Engine - Animate your views

tween-engine-big-logo.jpg

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 :p
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

  • TweenEngine.zip
    120.4 KB · Views: 1,090
  • TweenEngine1.1.zip
    121 KB · Views: 1,945
Last edited:

vb1992

Well-Known Member
Licensed User
Longtime User
I am getting an error with the tween sample 2.0
but not your first demo

B4X:
[Common.java:151:Log()] ** Activity (main) Create, isFirst = true **
[BA.java:82:uncaughtException()] main_rotatetween (java line: 531)
[BA.java:83:uncaughtException()] java.lang.NoSuchMethodError: android.widget.ImageView.getRotation
   at com.rootsoft.tweenengine.ImageViewAccessor.getValues(ImageViewAccessor.java:37)
   at com.rootsoft.tweenengine.ImageViewAccessor.getValues(ImageViewAccessor.java:1)
   at aurelienribon.tweenengine.Tween.build(Tween.java:786)
   at aurelienribon.tweenengine.Tween.build(Tween.java:79)
   at aurelienribon.tweenengine.BaseTween.start(BaseTween.java:85)
   at aurelienribon.tweenengine.TweenManager.add(TweenManager.java:61)
   at com.rootsoft.tweenengine.RSTweenManager.addTween(RSTweenManager.java:61)
   at com.tween.sample.njdude.main._rotatetween(main.java:531)
   at com.tween.sample.njdude.main._runanimation(main.java:564)
   at com.tween.sample.njdude.main._activity_create(main.java:222)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:136)
   at com.tween.sample.njdude.main.afterFirstLayout(main.java:84)
   at com.tween.sample.njdude.main.access$100(main.java:16)
   at com.tween.sample.njdude.main$WaitForLayout.run(main.java:72)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3694)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
   at dalvik.system.NativeStart.main(Native Method)
 

NJDude

Expert
Licensed User
Longtime User
Yes, as long as you don't use rotation, scale and fade it will work, but that limits the functionality of the library plus it will confuse people, so it's better to require Honeycomb or better.
 

walterf25

Expert
Licensed User
Longtime User
Error

Hi all, i'm trying to run NJDude's latest updated tweenEngine example project, and i keep receiving this error.

I'am running android 4.0.4 on MIUI's rom

any ideas, is anyone else having this issue?

error.jpg

thanks
Walter
 
Last edited:

NJDude

Expert
Licensed User
Longtime User
Hi all, i'm trying to run NJDude's latest updated tweenEngine example project, and i keep receiving this error.

I'am running android 4.0.4 on MIUI's rom

any ideas, is anyone else having this issue?

View attachment 13038

thanks
Walter

That is because you are compiling with SDK lower that 11 (I suggested to compile using SDK 16), however, if you don't want to upgrade, then open the Manifest editor and remove this line:
B4X:
android:hardwareAccelerated="true"

That should do it.
 
Last edited:

AurelienRibon

Member
Licensed User
Longtime User
I don't get it.

The restriction to android 3+ only concerns the "getScaleX/Y" methods. Why on earth can't just just remove these tween types to get the library working with android 2? That's 4 lines to remove. I just wrote the accessor to be used as an example, not to be used directly without even testing it.

Moreover, the main purpose of the Universal Tween Engine is to animate every possible object. Users provide their own accessors to let the engine interpolate their custom objects. As far as I can tell, the current B4A "port" is just a limited wrapper only providing support for View instances.

If the main issue to get the engine fully useable is that B4A can't work with raw java libraries, it seems a pretty heavy limitation just to be able to use VB instead of java langage. Is that the case?
 

NJDude

Expert
Licensed User
Longtime User
I don't get it.

The restriction to android 3+ only concerns the "getScaleX/Y" methods. Why on earth can't just just remove these tween types to get the library working with android 2? That's 4 lines to remove. I just wrote the accessor to be used as an example, not to be used directly without even testing it.

Moreover, the main purpose of the Universal Tween Engine is to animate every possible object. Users provide their own accessors to let the engine interpolate their custom objects. As far as I can tell, the current B4A "port" is just a limited wrapper only providing support for View instances.

If the main issue to get the engine fully useable is that B4A can't work with raw java libraries, it seems a pretty heavy limitation just to be able to use VB instead of java langage. Is that the case?

Hi AurelienRibon,

One thing I can tell you about this lib, IT'S AWESOME, the port to B4A works just fine, the issue with scale and rotation is not really a problem, I'm in the process of writing an app and used the Tween Lib for B4A and in order to avoid the problem with Android 2.x I just check for the version installed on the device and change the animation to one without scale or rotation, works like a charm, so, no need to remove those tweentypes.
 

AurelienRibon

Member
Licensed User
Longtime User
Okay, I though that the presence of the methods prevented to lib to compile. If it's a soft limitation, then everything's fine and I'm glad you can use it in your project then :)
 

sorex

Expert
Licensed User
Longtime User
for those who would like to test with fading options for images or text on 2.1 or 2.2 devices here's a workaround simular to the scaling trick I used in my first example.
 

Attachments

  • tweening_alpha.zip
    13.8 KB · Views: 304

RayLee

Member
Licensed User
Longtime User
B4X:
Unexpected event (missing RaiseSynchronousEvents): tweenend_onevent
Check the unfiltered logs for the full stack trace.

What happened?
B4X:
tweenerback.Initialize("tweenEnd")
            tweenerback.Listener = True
            tweener.registerPanelAccessor
            tweener.PanelTo(p, Constants.POSITION_XY, 30)
            tweener.setTarget(200, 300)
            tweener.Ease(Constants.ELASTIC_IN)
            tweener.Callback = tweenerback.TweenCallback
            tweener.Start
            tmrUpdate.Enabled = True
 
Top