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 に答える