implementing animation, get animation time

75 views
Skip to first unread message

Shay Tessler

unread,
Aug 12, 2014, 1:01:20 PM8/12/14
to codenameone...@googlegroups.com
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA: eclipse
Desktop OS: windows 7
Simulator : iphone 3gs
Device: ---


i want to make some animation , i want to make a loader ,

i have two questions 
1) dose animated gif will paint more fast the paint(Graphics g) ?
2) if i decide to choose CN1 animation how can i know how much time pass from the last paint , or get when cycle start stop .
     Some devices are faster and other are slower, if i want my animation to take 1000ms so i would like to know what step i am inside the animation . so of faster devices the animation will be more smooth . 

thank you
    
    

Shai Almog

unread,
Aug 13, 2014, 1:25:55 AM8/13/14
to codenameone...@googlegroups.com
Hi,
1. Not necessarily.

2. The animate() method is invoked with every EDT "cycle" which depends on the framerate defined in display. Currently its throttled to 60fps but it will be much lower for most cases.
Generally you should animate the state in animate() and draw in the paint.

Shay Tessler

unread,
Aug 13, 2014, 3:54:07 AM8/13/14
to codenameone...@googlegroups.com
so how can i know what step i am in the animation ?
its a thing i should manage my self with calculation of "system.currenttimemillis()"  ?

having this AIP could be nice :-)
https://java.net/projects/timingframework/pages/Home

Shai Almog

unread,
Aug 13, 2014, 10:40:08 AM8/13/14
to codenameone...@googlegroups.com
That's a dead project.

You can rely on animate() being invoked in a fixed interval so for most animations you won't really need to look at the current time.
To control position in the animation use the Motion class which allows you to move a position (number) using various type of motion behaviors over a course of time.

Shay Tessler

unread,
Aug 13, 2014, 6:07:09 PM8/13/14
to codenameone...@googlegroups.com
thanks shai, 
i have no need of a motion in my animation, just want to know where i am in the animation cycle . my animation repeats it self 
if my cycle duration is one second , and i will divide it to lets say 100 steps ,so i now can draw a step of the animation
something like the attached example will be good/fast enough? OR there is something build in into CN1 ?
thanks again :)

public class LoadMore extends Component implements Animation {

    private long  m_prevTime        = 0; //Dummy initialized the previous time, more accurate values will be inside the paint methos 
    private long  m_animationSteps  = 100;//How many steps in this animation
    private long  m_animationStep   = 0;  //Current step if this animation, FROM 0 TO m_animationSteps  
    private long  m_timeStep;             //How much time it takes to each step, calculated by the duration   

    
    private Dimension m_preSize = new Dimension(100, 40);// loaded should have a fixed size!
    
    @Override
    protected Dimension calcPreferredSize(){
        return m_preSize;
    }
   
          /**
    * @param duration  the duration of the animation
    */
          public LoadMore(long duration) {
        m_timeStep = duration / m_animationSteps;
    }
    
    public boolean animate(){
        return true;
    }

    public void paint(Graphics g){
        long currenttime = System.currentTimeMillis();
        long diff = currenttime -  m_prevTime;
        long stepsPassed = diff / m_timeStep;
        m_animationStep += stepsPassed;
        if(m_animationStep > m_animationSteps) {
            m_animationStep = 0;
        }

        /*
         * DO THE PAINT HERE !!!
         */
        
        
        m_prevTime = currenttime;
    }




Shay Tessler

unread,
Aug 13, 2014, 6:38:24 PM8/13/14
to codenameone...@googlegroups.com
also i guess i am doing something wrong ........
i don't see any of my paints , for start i want to draw an circle the going from small to big ans big to small each cycle

        /*
         * DO THE PAINT HERE !!!
         */

       
int arcSize = (int)m_animationStep;
       
if(m_animationStep > (m_animationSteps/2)) {
            arcSize
= (int)(m_animationSteps - m_animationStep);
       
}
        g
.setColor(Color.RED);
        g
.fillArc(50, 50, arcSize, arcSize, 0, 270);
         
       
//END THE PAINT HERE

        m_prevTime = currenttime;

Shai Almog

unread,
Aug 14, 2014, 2:39:56 AM8/14/14
to codenameone...@googlegroups.com
Do you see the circle in the simulator?
What's color.RED?

In animate just do size++; if size > x size = 0;
Then in paint just paint based on size.
See the InfiniteProgress class for a sample of how to do something like this.

Shay Tessler

unread,
Aug 14, 2014, 7:34:44 AM8/14/14
to codenameone...@googlegroups.com
color red its just a "final static int RED = 0xD23939" inside a class called Color.
if the cycle was after the middle then the circle should start getting smaller  e.g if(m_animationStep > (m_animationSteps/2)) { arcSize = (int)(m_animationSteps - m_animationStep); }

ok i got it working, very cool animation  :) i will finish it and share with you
also fix the proffered size to see it working
private Dimension m_preSize = new Dimension(100, 100);// loaded should have a fixed size!

        /*
         * DO THE PAINT HERE !!!
         */

        g
.setAntiAliased(true);

       
int arcSize = (int)m_animationStep;
       
if(m_animationStep > (m_animationSteps/2)) {
            arcSize
= (int)(m_animationSteps - m_animationStep);
       
}

        g
.setColor(0xD23939);
       
int location = 60 - (arcSize / 2);
        g
.fillArc(location, location, arcSize, arcSize, 0, 360);
       
//END THE PAINT HERE

Shay Tessler

unread,
Aug 14, 2014, 10:20:27 AM8/14/14
to codenameone...@googlegroups.com
ok i have finished, hope u like it....

public class LoadMore extends Component implements Animation {

   
private long  m_prevTime        = 0; //Dummy initialized the previous time, more accurate values will be inside the paint methos

   
private long  m_animationSteps  = 400;//How many steps in this animation

   
private long  m_animationStep   = 0;  //Current step if this animation, FROM 0 TO m_animationSteps  
   
private long  m_timeStep;             //How much time it takes to each step, calculated by the duration  


   
   
private Dimension m_preSize = new Dimension(130, 30);// loaded should have a fixed size!

   
   
@Override
   
protected Dimension calcPreferredSize(){
       
return m_preSize;
   
}
   
         
/**
    * @param duration  the duration of the animation
    */

   
public LoadMore() {
       
int duration = 2800;

        m_timeStep
= duration / m_animationSteps;
   
}
   
   
public boolean animate(){
       
return true;
   
}

   
public void paint(Graphics g){
       
long currenttime = System.currentTimeMillis();
       
long diff = currenttime -  m_prevTime;
       
long stepsPassed = diff / m_timeStep;
        m_animationStep
+= stepsPassed;
       
if(m_animationStep > m_animationSteps) {
            m_animationStep
= 0;
       
}

       
       
/*

         * DO THE PAINT HERE !!!
         */

        g
.setAntiAliased(true);
        g
.setColor(0xD23939);
       
       
long animationElements = 5;
       
long animationItemSteps = m_animationSteps / animationElements;
       
for(int index = 0 ; index < animationElements-1 ; index++) {
           
int arcSize = 0;
           
int itemStep = (int)(animationItemSteps * index);
           
if(m_animationStep > itemStep) {
                arcSize
= (int)m_animationStep - itemStep;
               
if(arcSize > animationItemSteps) {
                    arcSize
=  (int)animationItemSteps - (arcSize - (int)animationItemSteps);
               
}
           
}
           
int newArcSize = arcSize / 4; //Need to fix the size is calculated by the timeing......
           
if(newArcSize < 5) {
                newArcSize
= 5;
           
}          
           
double locationX = index*28 + 20.0 - (newArcSize / 2.0) ;  
           
double locationY = 50  - (newArcSize / 2);
            g
.fillArc((int)locationX, (int)locationY, (int)newArcSize, (int)newArcSize, 0, 360);
           
       
}
       
/*
         * END THE PAINT HERE
         */


        m_prevTime
= currenttime;

   
}

}

Reply all
Reply to author
Forward
0 new messages