问题
I tried like this, which i found on the net to play gif images:
private class MYGIFView extends View {
Movie movie;
InputStream is = null;
long moviestart;
public MYGIFView(Context context) {
super(context);
// Provide your own gif animation file
is = context.getResources().openRawResource(R.drawable.mygif);
movie = Movie.decodeStream(is);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
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());
System.out.println("time=" + relTime + "\treltime="
+ movie.duration());
movie.setTime(relTime);
movie.draw(canvas, this.getWidth() / 2 - 20,
this.getHeight() / 2 - 40);
this.invalidate();
}
Though many have said that they got their desired result.
I have put the gif image in drawable.
I am getting movie.duration() null.
Please suggest where am i wrong or if is there any way.
回答1:
Android cannot play GIF files without WebView. You must break it apart into frames and animate it yourself.
I found on another StackOverflow post this bit of software from XoyoSoft, called GifSplitter, that can split a GIF into frames. You would then want to use AnimationDrawable to combine those.
It would look something like this:
// Your files:
res\drawable-xxxx\frame1.jpg
res\drawable-xxxx\frame2.jpg
res\drawable-xxxx\frame3.jpg
// ...
res\drawable-xxxx\frame99.jpg
Then, there is an example in the AnimationDrawable documentation above that will show how to display those images.
Lastly, you must load and play the animation; that, too, is detailed in the AnimationDrawable documentation.
来源:https://stackoverflow.com/questions/11806320/play-gif-image-in-android-without-using-webview