8

I created a custom view from scratch. Extended View and overrided onDraw(). When comes down in animating the view i generate a custom animation using offsets. eg.

while(!isOnTop){
mOffset++;

//draw the component a a it higher using the offset

if(position == 0)
isOnTop==true;
invalidate();

}

The thinking is that my frames come from invalidate it self. The problem is that invalidation of this view can come just by scrolling a listview at the same screen.

This "shared invalidation()" causes lag to my animation.So is there a way out of that lag?

Do you have any other suggestion of performing animations in that shared enviroment? Creating an animation using a seperate thread that calculates the offset also needs forced invalidation() calls to display the animation (correct me if i'm wrong).

Is the only solution to perform the animation in eg 10 invalidation requests with a larger step? It will ease the lag out but i think i can use a different approach on that.

2 Answers 2

22
+50

"What is best" of course depends greatly on exactly what you are trying to do. You haven't said what you are trying to accomplish, so we can only guess at what may be best for you.

Here are some simple things:

Here's a simple repeated invalidate using Handler:

long mAnimStartTime;

Handler mHandler = new Handler();
Runnable mTick = new Runnable() {
    public void run() {
        invalidate();
        mHandler.postDelayed(this, 20); // 20ms == 60fps
    }
}

void startAnimation() {
    mAnimStartTime = SystemClock.uptimeMillis();
    mHandler.removeCallbacks(mTick);
    mHandler.post(mTick);
}

void stopAnimation() {
    mHandler.removeCallbacks(mTick);
}
3
  • 1
    invalidate() is not instant. Can take more than 20ms because there are a lot of views needed to invalidate.The view has to wait till all of them are finished (at least pre 3.0). postDelayed will then introduce more lag. Limit fps that way is not good.Correct me if I'm wrong.
    – weakwire
    Oct 17, 2011 at 16:00
  • 2
    Not really. If invalidate() is taking more than 20ms, you have serious problems in how you have structured your view hierarchy. The vast majority of animations within windows in the standard platform use regular views with invalidate(), and can achieve 60fps. You just need to make sure that you don't do things in your view hierarchy like have multiple big opaque bitmaps on top of each other or scaling that making drawing them slow... but you don't want to do that anyway.
    – hackbod
    Oct 18, 2011 at 3:14
  • Don't fps depend on hardware and current cpu/gpu/memory load? I.e. if consistent animation time is important, I think one should (efficiently) measure fps on runtime and determine the delay time accordingly.
    – ılǝ
    May 2, 2014 at 7:27
4

Since this question has some interest I will reply.

The best way to to that is to have a separate canvas thread. A "separate" canvas can only be achieved with a SurfaceView. LunarLanding is an excelent example of that use. Each frame is calculated separately than the main view sharing only CPU time, not drawing time. Therefore is faster, even with the combination of for e.g a regular view at the top and an animating view at the bottom.

But you have to set an interval if you are in that shared environment. That interval is used for the FPS cap. If you don't set FPS cap then the CPU will running wild managing to get good animation to the SurfaceView if it was alone. Capping it at 60fps or even less will do the trick to draw all views efficiently with no CPU overload.

So see the drawing thread of the Lunar Landing from the API demos and set a FPS cap.

private long timeNow;
    private long timeDelta;
    private long timePrevFrame;

    private void capFps(int fps) {  
            timeNow = System.currentTimeMillis();
            timeDelta = timeNow - timePrevFrame;        

            try {

//ps you can always set 16 instead of 1000/fps for 60FPS to avoid the calculation every time

Thread.sleep((1000 / fps) - timeDelta);
            } catch (InterruptedException e) {

            }   

            timePrevFrame = System.currentTimeMillis();
    }

and then the drawing thread will look something like this:

@Override
    public void run() {
        Canvas c;

        while (run) {
            c = null;
            sleepFps(60, false);
            try {
                synchronized (surfaceHolder) {
                    c = surfaceHolder.lockCanvas(null);
                    widgetView.doDraw(c);
                }
            } finally {
                if (c != null) {
                    surfaceHolder.unlockCanvasAndPost(c);
                }
            }

        }
    }
3
  • Sorry, this is way more complicated than often needed, and a SurfaceView has many compromises that make it often not the best solution.
    – hackbod
    Oct 16, 2011 at 21:58
  • Yes it is but is the best one to have multi animated views together. the Question was what is the "Best" way, although thank you for the answer
    – weakwire
    Oct 17, 2011 at 2:00
  • This one is actually the way I'm familiar with from j2me. If you don't like the calculations, you can always skip the it and sleep for constant time.
    – Vitali Pom
    Aug 30, 2013 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.