Android Question Animation Fade in and out

Hi,
I downloaded the trial version today and really enjoying the experience. I am particularly interested in understanding how the animation works with this software.

I referred to this link https://www.b4x.com/android/forum/threads/android-views-animation-tutorial.6967/

My scenario is this.
All I want is a button which when clicked would slowly show some text and which should fade away after sometime. Hopefully in future when I click on a cat, it would say meow meow and that fades away.

The link above only provides detail on moving from (0 to 1) or (1 to 0) but not (0 to 1 to 0) or (1 to 0 to 1)

I tried the following way but it doesn't seem to work.

B4X:
Sub Button_Click
    Dim b As Button
    b = Sender
    b.Text = "Meow Meow"
    'Safety check. Not really required in this case.
    If Not(b.Tag Is Animation) Then Return
    Dim a As Animation
    a.InitializeAlpha("", 1, 0)
    a.Duration = 1000
    a = b.Tag
    a.Start(b)
   'Do I again have to initialize alpha?
    a.InitializeAlpha("",0, 1)
    a.Duration = 1000
    a = b.Tag
    a.Start(b)
End Sub

Please let me know your thoughts!
Thank you.
 

sorex

Expert
Licensed User
Longtime User
I never used the animation you refer to but I guess you need to add a 1 second delay to the second one as they are now going at the same time.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Cheats way .. Simple label on a button. :)
 

Attachments

  • MeowMeow Test.zip
    58.6 KB · Views: 322
Upvote 0

ilan

Expert
Licensed User
Longtime User
are you trying to animate fade out and fade in at the same button_click event?
it doesn't make sense since it is calling the fade in and fade out at the same time so you should get no effect like this (i guess)

if you want to fade out and the fade in after fade out is finished you should do it in a different way:

B4X:
Sub Process_Globals
    Dim fade_timer As Timer
End Sub

Sub Globals
    Dim fadeBtn, b(10) As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    fade_timer.Initialize("fadetimer",100)
    For i = 0 To b.Length -1
        b(i).Initialize("Button")
        b(i).Text = "Button " & i
        Activity.AddView(b(i),0,i*50dip,150dip,50dip)
    Next
End Sub

Sub Button_Click
    Dim btn As Button = Sender
    btn.SetVisibleAnimated(1000, False)
    fadeBtn = btn
    fade_timer.Interval = 1000
    fade_timer.Enabled = True
End Sub

Sub fadetimer_Tick
    fade_timer.Enabled = False
    fadeBtn.SetVisibleAnimated(1000,True)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

btw do you have more then 1 button with the same event name? if no then you don't need "Dim btn As Button = Sender"
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Animation has à finished event, no need to use timers...
Denife animation1 as fade in, set a flag to know if you are fading in or out, start fadein, and in animation1_finished define animation1 as fade out, set the flag accordingly, all this inside a fade flag check...
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Thank you very much guys!!
Instead of animation, I will look into timer.

You also need to know that the animation lib is not really changing the view properties.

It is only drawing an animation but not always it is what we really want.
you can check it by adding a translation animation to a view (like button) via animation library
with interval of 2000 and also start at the same time a timer with interval 100 and in timer_tick event check the
view position with log(btn.left) you should get the same position for the whole animation.

so if you really want to move a view you better use .SetLayoutAnimated.
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
Animation has à finished event, no need to use timers...
Denife animation1 as fade in, set a flag to know if you are fading in or out, start fadein, and in animation1_finished define animation1 as fade out, set the flag accordingly, all this inside a fade flag check...

from my experience it will not work probably.
i used a lot in the past the animation lib and also animation plus lib from @Informatix and i had not always good result on some devices.
sometimes when the animation ended the view was seen for a short time on his original position and then again animated back.

because the animation lib does not really change the view properties.
after erel has updated b4a (i think 3.5) and added support to

.SetVisibleAnimated
.SetLayoutAnimated
.SetColorAnimated
....

i think it is the best solution to make simple animations to your views. (my opinion :))
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I agree that for more complex animations, the Animation Lib may not be the best choice, but I have used a lot of FadeIN/Out with it with no issues... A correct check of current state is primal, but it does provide good results
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
I agree that for more complex animations, the Animation Lib may not be the best choice, but I have used a lot of FadeIN/Out with it with no issues... A correct check of current state is primal, but it does provide good results

That is true. The fade in/out should work without any issues. For the other animations i ithink it is better to use the build in animation support that also does not require any library. :)
 
Upvote 0
Top