" quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
1. <ImageViewandroid:id="@+id/gifpicture"
2. android:layout_width="fill_parent"
3. android:layout_height="wrap_content"
4. android:src="@drawable/animation" />
程序只能加载GIF动画的第一帧。效果如下:
如果想要正常播放GIF动画,需要借助Movie实现。写了一个简单示例,程序目录结构如下:
animation.gif是需要播放的GIF动画。
MainActivity实现了加载GIF动画的功能。
1. publicclassMainActivity extendsActivity {
2.
3. privateMovie mMovie;
4. privatelongmMovieStart;
5.
6. @Override
7. publicvoidonCreate(Bundle savedInstanceState) {
8. super.onCreate(savedInstanceState);
9. setContentView(newCustomGifView(this));
10. }
11. classCustomGifView extendsView {
12. publicCustomGifView(Context context) {
13. super(context);
14. mMovie = Movie.decodeStream(getResources().openRawResource(
15. R.drawable.animation));
16. }
17.
18. publicvoidonDraw(Canvas canvas) {
19. longnow = android.os.SystemClock.uptimeMillis();
20.
21. if(mMovieStart == 0) { // first time
22. mMovieStart = now;
23. }
24. if(mMovie != null) {
25.
26. intdur = mMovie.duration();
27. if(dur == 0) {
28. dur = 1000;
29. }
30. intrelTime = (int) ((now – mMovieStart) % dur);
31. mMovie.setTime(relTime);
32. mMovie.draw(canvas, 0, 0);
33. invalidate();
34. }
35. }
36. }
37. }
内部类CustomGifView 利用Movie播放GIF动画。解释一下:
1. mMovie = Movie.decodeStream(getResources().openRawResource(R.drawable.animation));
将GIF动画以文件流的形式转换成Movie。
1. publicvoidonDraw(Canvas canvas) {
2. longnow = android.os.SystemClock.uptimeMillis();
3.
4. if(mMovieStart == 0) { // first time
5. mMovieStart = now;
6. }
7. if(mMovie != null) {
8.
9. intdur = mMovie.duration();
10. if(dur == 0) {
11. dur = 1000;
12. }
13. intrelTime = (int) ((now – mMovieStart) % dur);
14. mMovie.setTime(relTime);
15. mMovie.draw(canvas, 0, 0);
16. invalidate();
17. }
18. }
invalidate();作用是刷新当前View,这样onDraw方法重复执行,Movie就能画出GIF动画的每一帧。到此,GIF动画就能正常播放了。
源代码见:
http://bigcateasymorse.googlecode.com/svn/trunk/android_gif_demo/
标签:动画,mMovie,Movie,mMovieStart,GIF,Android,dur From: https://blog.51cto.com/u_548275/6237984