一、初始化:
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();}}}