问题描述
我试过这样的,我在网上找到播放GIF图像:
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(); }
虽然很多人说他们得到了所需的结果.
我把gif图像放在可绘制中. 我得到movie.duration()null.
请建议我错了,或者如果有没有办法.
推荐答案
android无法在没有WebView的情况下播放gif文件.你必须将其分开进入框架并自己动画.
我在另一个stackoverflow post 来自xoyosoft 的这一点,称为gifsplitter,可以拆分一个gif进入框架.然后,您希望使用 AnimationDrawable 组合那些.
它看起来像这样:
// Your files: res\drawable-xxxx\frame1.jpg res\drawable-xxxx\frame2.jpg res\drawable-xxxx\frame3.jpg // ... res\drawable-xxxx\frame99.jpg
然后,在上面的AnimationDrawable文档中存在一个例子,该示例将显示如何显示这些图像.
最后,您必须加载和播放动画;在AnimationDrawable文档中详细说明.
问题描述
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.
推荐答案
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.