使用GIF图像表面观? [英] Use Gif image in Surface view?

查看:187
本文介绍了使用GIF图像表面观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,其中我使用具有以下code一个GIF图片

I am developing an android application , in which i am using a gif images with the following code

private static byte[] streamToBytes(InputStream is) {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    byte[] buffer = new byte[1024];
    int len;
    try {
        while ((len = is.read(buffer)) >= 0) {
            os.write(buffer, 0, len);
        }
    } catch (java.io.IOException e) {
    }
    return os.toByteArray();
}

//在类的构造函数中的

// In constructor of the class

is = this.context.getResources().openRawResource(R.drawable.fondo_juego_nexus);

            if (DECODE_STREAM) {
                  System.out.println("in if DECODE_STREAM");
                mMovie = Movie.decodeStream(is);
            } 

            else {
                byte[] array = streamToBytes(is);
                System.out.println("in else  DECODE_STREAM");
                mMovie = Movie.decodeByteArray(array, 0, array.length);
           }



// In On Draw 

  long now = android.os.SystemClock.uptimeMillis();

        if (mMovieStart == 0) {   // first time
            mMovieStart = now;
        }
        if (mMovie != null) {
            System.out.println("in if (mMovie != null)  " + mMovie.duration());
            int dur = mMovie.duration();
            if (dur == 0) 
            {
                dur = 1000;
                System.out.println("in if movie if");
            }
            System.out.println("duration is  "+ dur);
            int relTime = (int)((now - mMovieStart) % dur);
            mMovie.setTime(relTime);
            System.out.println("in if displaying syd");
            mMovie.draw(canvas,120,100);

        }

和由活动ontouch我退出像

and by ontouch i exit from activity like

else if(_y<=60 &&  _x<=60)
         {


             sp.play(mySound1, volume, volume, 0, 0, 1);
             sp.release();
            playr.stop();
             tme.cancel();
             act.finish();

}

卜当我退出与上述方法活动,回到previous活动,
并再次到我使用GIF图像不会出现在活动来,在设备星系S2,2.3.3但是2.2同样大小的仿真器是细

Bu when i exit activity with above method and go back to previous activity , and come again to the activity where i am using gif image it does not appear , on device galaxy s2 , 2.3.3 but on an emulator of same size of 2.2 it is fine

有没有这种方法,或用什么方式我可以用它来显示GIF图像的任何问题。

is there any problem with this approach or what way i can use to display a gif image

我应该怎么办消除这种误差

what should i do to remove this error

推荐答案

我得到了我的问题的解决方案,我不知道它有多好,
但它的工作对我来说早些时候,我使用这个构造code

I got the solution for my problem , i do not know how good it is , but it worked for me earlier i was using this code in constructor

 if (DECODE_STREAM) {
                mMovie = Movie.decodeStream(is);
            } else {
                byte[] array = streamToBytes(is);
                mMovie = Movie.decodeByteArray(array, 0, array.length);
            }   

        }

这是workign精第一次,但我认为,在第二次它是由于运行内存
太多这是含蓄地运行垃圾收集器
和我的形象初始化也一次又一次地运行这是的原因
本场比赛的糟糕表现,以及
我所做的就是我刚才添加
手动垃圾收集

It was workign fine first time, but i think in second time it was running out of memory due to too much of garbage collector which was running implicitly and my image initialization was also running again and again which was the cause of the bad performance of the game ,and what i did was that i just added the garbage collector manually with

System.gc();

和初始化后,这也是所有图像
放在下面code

and initialized all images after this and also placed the following code

if (DECODE_STREAM) {
                mMovie = Movie.decodeStream(is);
            } else {
                byte[] array = streamToBytes(is);
                mMovie = Movie.decodeByteArray(array, 0, array.length);
            }   

        }

在此之后
这就避免了垃圾收集器的运行隐含

after this which avoided the implicit running of garbage collector

现在的gif图片是工作的罚款

and now the gif image is working fine

这篇关于使用GIF图像表面观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆