Android 中 Movie 类显示GIF图片

1.Movie类简介

技术分享

 

2.代码实现

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyGifView(this));
    }

    
    class MyGifView extends View{
        
        //movie管理gif图片里的多个帧
        private Movie movie;
        //表示开始播放gif图片的绝对时间
        private  long movieStart;
        
        public MyGifView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public MyGifView(Context context) {
            super(context);
        
            movie = Movie.decodeStream(getResources().openRawResource(R.drawable.maidi3));
            
        }
        
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            long currentTime = android.os.SystemClock.uptimeMillis();
            
            //第一次播放
            if(movieStart == 0){
                movieStart = currentTime;
            }
            
            //循环播放
            if(movie != null){
                int duration = movie.duration();
                int relTime = (int)((currentTime - movieStart)%duration);
                movie.setTime(relTime);
                movie.draw(canvas,10,20);
//                Paint paint = new Paint();
//                paint.setColor(getResources().getColor(android.R.color.holo_green_light));
//                movie.draw(canvas,10,20, paint);
                invalidate();
            }
        }
        
    }
    
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。