zoukankan      html  css  js  c++  java
  • 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();
                }
            }
            
        }
        
    }

    3.知识点说明

    很多情况下,不管是我们自己使用时间间隔来做一些算法,或是调用系统的API,比如动画效果,都会需要基于时间间隔来做,通常做法是:记录开始时间 startTime,然后每次回调时,获取当前时间  currentTime,计算差值 = currentTime - startTime,而获取当前时间,系统提供了两种方法:

    SystemClock.uptimeMillis 和 System.currentTimeMillis

    这两种方法有何区别呢?

    1. SystemClock.uptimeMillis()  // 从开机到现在的毫秒数(手机睡眠的时间不包括在内);

    2. System.currentTimeMillis() // 从1970年1月1日 UTC到现在的毫秒数;

    但是,第2个时间,是可以通过System.setCurrentTimeMillis修改的,那么,在某些情况下,一但被修改,时间间隔就不准了。

    特别说明点:AnimationUtils 中明确说了:

    [java]
    /**
     * Returns the current animation time in milliseconds. 
     * This time should be used when invoking
     * {@link Animation#setStartTime(long)}. Refer to 
     * {@link android.os.SystemClock} for more
     * information about the different available clocks. 
     * The clock used by this method is
     * <em>not</em> the "wall" clock (it is not 
     * {@link System#currentTimeMillis}).
     *
     * @return the current animation time in milliseconds
     *
     * @see android.os.SystemClock
     */ 

  • 相关阅读:
    5、文件处理
    6、Python模块
    4、字典使用
    3、列表 list
    1、Python基础
    2、循环判断
    配置LOG4J(log4j-1.2.17)
    File /WEB-INF/web.xml not found...
    关于TOMCAT的 ROOT/WEB-INF/web.xml的配置
    tomcat 配置系列3
  • 原文地址:https://www.cnblogs.com/d-on/p/4201768.html
Copyright © 2011-2022 走看看