简体   繁体   中英

uneven frame rate when making animation using BitmapFactory.decodeByteArray

I want to play a frame by frame animation. I have a ArrayList byeArr that contains jpeg pictures and decodes them one by one to show on a surfaceview.

tim = new CountDownTimer(1,1)
{
int i = 0;

@Override
public void onFinish()
{

    if ( i < byeArr.size() )
    {
        bitMap = BitmapFactory.decodeByteArray ( byeArr.get(i) , 0,  byeArr.get(i).length );
            /// bitMap is a bitmap that is displayed on screen.

        lastFrameMs = System.currentTimeMillis() - lastFrameSysTime;
        totalFr = totalFr + lastFrameMs;
        Log.e("LF", "frame: "+ String.valueOf(i) + " = " + String.valueOf(lastFrameMs) +"ms");

        lastFrameSysTime = System.currentTimeMillis();
        i++;
        tim.start();

    }
...

For example, if I would want to have a 15 FPS frame rare, I should set CountDownTimer to 1000/ 15 - lastFrameMs , where lastFrameMs would be time to draw one frame.

This would work nice, it the frames would be drawn at a constant speed, yet the problem is that they do not. Each frame can take everywhere from 25 to 65 ms...

11-14 19:00:52.402: E/LF(26270): frame: 0 = 60ms
11-14 19:00:52.429: E/LF(26270): frame: 1 = 27ms
11-14 19:00:52.488: E/LF(26270): frame: 2 = 55ms
11-14 19:00:52.519: E/LF(26270): frame: 3 = 32ms
11-14 19:00:52.574: E/LF(26270): frame: 4 = 57ms
11-14 19:00:52.605: E/LF(26270): frame: 5 = 30ms
11-14 19:00:52.664: E/LF(26270): frame: 6 = 56ms
11-14 19:00:52.695: E/LF(26270): frame: 7 = 33ms
11-14 19:00:52.753: E/LF(26270): frame: 8 = 58ms
11-14 19:00:52.824: E/LF(26270): frame: 9 = 71ms
11-14 19:00:52.886: E/LF(26270): frame: 10 = 62ms
11-14 19:00:52.914: E/LF(26270): frame: 11 = 27ms
11-14 19:00:52.976: E/LF(26270): frame: 12 = 62ms
11-14 19:00:53.007: E/LF(26270): frame: 13 = 29ms
11-14 19:00:53.066: E/LF(26270): frame: 14 = 61ms
11-14 19:00:53.097: E/LF(26270): frame: 15 = 27ms
11-14 19:00:53.171: E/LF(26270): frame: 16 = 77ms
11-14 19:00:53.199: E/LF(26270): frame: 17 = 28ms
11-14 19:00:53.261: E/LF(26270): frame: 18 = 62ms
11-14 19:00:53.300: E/LF(26270): frame: 19 = 37ms
11-14 19:00:53.355: E/LF(26270): frame: 20 = 56ms
11-14 19:00:53.390: E/LF(26270): frame: 21 = 34ms
11-14 19:00:53.457: E/LF(26270): frame: 22 = 66ms
11-14 19:00:53.484: E/LF(26270): frame: 23 = 30ms
11-14 19:00:53.550: E/LF(26270): frame: 24 = 64ms
11-14 19:00:53.582: E/LF(26270): frame: 25 = 31ms
11-14 19:00:53.640: E/LF(26270): frame: 26 = 60ms
11-14 19:00:53.667: E/LF(26270): frame: 27 = 29ms
11-14 19:00:53.734: E/LF(26270): frame: 28 = 66ms
11-14 19:00:53.769: E/LF(26270): frame: 29 = 33ms
11-14 19:00:53.828: E/LF(26270): frame: 30 = 60ms
11-14 19:00:53.871: E/LF(26270): frame: 31 = 43ms
11-14 19:00:53.929: E/LF(26270): frame: 32 = 58ms
11-14 19:00:53.933: E/- - -(26270):  - - - 
11-14 19:00:53.933: E/fps(26270): 21 FPS
11-14 19:00:53.933: E/avrg ms for frame(26270): 46

... any ideas what could be done about it?

Thanks!

You should not display them as soon they are decoded.

In your timer, decode a bitmap then wait a time X before displaying the bitmap. Where X is

X = constantInterval - lastFrameMs.

if X is negative, then draw immediately.

Also, you should consider storing your animation in RAM more than decoding it, if it's small enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM