我正在玩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();
}
}
}