自制android gif動畫解析器

銳湃發表於2015-10-08

GifSurfaceView

package com.tz.dream.pa.gifanimation;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class GifSurfaceView extends SurfaceView implements Callback {

	private SurfaceHolder holder;

	// gif圖片路徑
	private String path;

	private Movie movie;

	// 放大倍數
	private int zoom = 1;

	// 執行動畫
	private Handler handler = new Handler();

	// 執行緒
	private Runnable runnable = new Runnable() {

		@Override
		public void run() {
			// 獲取畫布(加鎖)
			Canvas canvas = holder.lockCanvas();

			canvas.save();

			canvas.scale(zoom, zoom);

			// 畫圖片(設定圖片顯示的位置)
			movie.draw(canvas, 0, 0);

			// 逐幀繪製圖片(圖片數量5)
			// 1 2 3 4 5 6 7 8 9 10
			// 1 2 3 4 0 1 2 3 4 0
			movie.setTime((int) (System.currentTimeMillis() % movie.duration()));

			canvas.restore();

			// 畫畫完成(解鎖)
			holder.unlockCanvasAndPost(canvas);

			handler.postDelayed(runnable, 50);
		}
	};

	public void setPath(String path) {
		this.path = path;
	}

	public void setZoom(int zoom) {
		this.zoom = zoom;
	}

	public GifSurfaceView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initParam();
	}

	public GifSurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initParam();
	}

	public GifSurfaceView(Context context) {
		super(context);
		initParam();
	}

	/**
	 * 初始化引數
	 */
	private void initParam() {
		holder = getHolder();
		holder.addCallback(this);

		// handler = new Handler();
	}

	/**
	 * 計算檢視寬高
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		// 載入gif圖片
		// 1、獲取gif圖片路徑
		if (!TextUtils.isEmpty(path)) {
			try {
				// 載入gif圖片
				InputStream stream = getContext().getAssets().open(path);
				movie = Movie.decodeStream(stream);
				// 獲取gif圖片寬高
				int width = movie.width();
				int height = movie.height();
				setMeasuredDimension((int)(width*zoom), (int)(height*zoom));
				// 執行gif動畫
				handler.post(runnable);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {

	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// 停止gif動畫
		handler.removeCallbacks(runnable);
	}

}

MainActivity

package com.tz.dream.pa.gifanimation;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		GifSurfaceView gifSurfaceView = (GifSurfaceView) findViewById(R.id.gsv);
		gifSurfaceView.setPath("mn.gif");
	}

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <com.tz.dream.pa.gifanimation.GifSurfaceView
        android:id="@+id/gsv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

資源圖片



整理自教程

相關文章