博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 中 Movie 类显示GIF图片
阅读量:4330 次
发布时间:2019-06-06

本文共 2659 字,大约阅读时间需要 8 分钟。

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 中明确说了:

[]

/**
 * Returns the current animation time in milliseconds. 
 * This time should be used when invoking
 * {@link Animation#setStartTime()}. 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
 */ 

 

转载于:https://www.cnblogs.com/d-on/p/4201768.html

你可能感兴趣的文章
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
Centos 7 Mysql 最大连接数超了问题解决
查看>>
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>