0

我正在玩gif animation我的应用程序。它工作得很好,但如果我玩了这么多次animation,它就会扔进nullpointer exception电影对象。

关于这个问题的任何建议。下面是我的代码

SampleView.class:

 class SampleView extends View {
           java.io.InputStream is;


            private Movie mMovie;
            private long mMovieStart;

            private  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();
            }

            public SampleView(Context context) {
                super(context);



                    if(is==null)
                    {

                try {
                    is =context.getAssets().open("a.gif");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    }


                    mMovie = Movie.decodeStream(is);

            }

            public SampleView(Context context, AttributeSet attrs){
                super(context,attrs);

                    if(is==null)
                    {

                try {
                    is =context.getAssets().open("a.gif");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    }

                mMovie = Movie.decodeStream(is);
            }
            public SampleView(Context context, AttributeSet attrs, int defStyle){
                super(context,attrs,defStyle);

                    if(is==null)
                    {

                try {
                    is =context.getAssets().open("a.gif");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    }

                    mMovie = Movie.decodeStream(is);
            }


            @Override protected void onDraw(Canvas canvas) {

                long now = android.os.SystemClock.uptimeMillis();
                if (mMovieStart == 0) {   // first time
                    mMovieStart = now;
                }
                if (mMovie != null) {
                    int dur = mMovie.duration();
                    if (dur == 0) {
                        dur = 1000;
                    }
                    int relTime = (int)((now - mMovieStart) % dur);
                    mMovie.setTime(relTime);
                    mMovie.draw(canvas, getWidth() - mMovie.width(),
                                getHeight() - mMovie.height());
                    invalidate();
                }
            }
        } 
4

1 回答 1

0

尝试这个,

public class GIFDemo extends GraphicsActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new GIFView(this));
}

private static class GIFView extends View {

    Movie movie;
    InputStream is = null;
    long moviestart;
    long moviestart1;

    public GIFView(Context context) {
        super(context);
        is = context.getResources()
                .openRawResource(R.drawable.animated_gif);

        movie = Movie.decodeStream(is);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFCCCCCC);
        super.onDraw(canvas);
        long now = android.os.SystemClock.uptimeMillis();
        System.out.println("now=" + now);
        if (moviestart == 0) { // first time
            moviestart = now;

        }

        System.out.println("\tmoviestart=" + moviestart);
        int relTime = (int) ((now - moviestart) % movie.duration());
        // int relTime1 = (int) ((now - moviestart1) % movie1.duration());
        System.out.println("time=" + relTime + "\treltime="
                + movie.duration());
        movie.setTime(relTime);
        // movie1.setTime(relTime1);
        movie.draw(canvas, 10, 10);
        // movie1.draw(canvas, 10, 100);
        this.invalidate();
    }
}
}

添加此 GraphicsActivity 类。

  class GraphicsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void setContentView(View view) {

    super.setContentView(view);
}
}
于 2013-02-27T06:06:22.560 回答