android Gif图片播放

来源:互联网 发布:淘宝付款取消验证码 编辑:程序博客网 时间:2024/06/10 07:09

一、初始化:

private void init() {movie = Movie.decodeStream(this.getResources().openRawResource(R.raw.test));}


二、不断的进行刷新:


@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);long now = android.os.SystemClock.uptimeMillis();if (mMovieStart == 0) { // first timemMovieStart = now;}if (movie != null) {int dur = movie.duration();if (dur == 0) {dur = 1000;}int relTime = (int) ((now - mMovieStart) % dur);movie.setTime(relTime);movie.draw(canvas,(getWidth()-movie.width())/2, (getHeight()-movie.height())/2);invalidate();}



完整代码:

package com.gif.activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.Movie;import android.util.AttributeSet;import android.view.View;public class MyView extends View {private long mMovieStart = 0;private Movie movie;public MyView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}public MyView(Context context, AttributeSet attrs) {super(context, attrs);init();}public MyView(Context context) {super(context);init();}private void init() {movie = Movie.decodeStream(this.getResources().openRawResource(R.raw.test));}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);long now = android.os.SystemClock.uptimeMillis();if (mMovieStart == 0) { // first timemMovieStart = now;}if (movie != null) {int dur = movie.duration();if (dur == 0) {dur = 1000;}int relTime = (int) ((now - mMovieStart) % dur);movie.setTime(relTime);movie.draw(canvas,(getWidth()-movie.width())/2, (getHeight()-movie.height())/2);invalidate();}}}




原创粉丝点击