您的位置:首页 > 移动开发 > Android开发

android显示gif动态图的方法

2013-09-27 18:11 357 查看
最近的项目有了这个需求,然后上网找了找,没有找到个非常完美的方案,google上那个开源项目GifView有oom的错误,由于项目紧急,就没有深入研究代码。后来在网上找到一段代码,说白了就是一个自定义的可播放gif图片的空间。

这里使用了movie来显示,有兴趣的可以自己去研究下google的api和源代码。


import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;

public class GifView extends View {
	private int gifResource;
	private Movie mMovie;
	private long movieStart;

	public GifView(Context context, byte[] data) {
		super(context);
		// initializeView();
		setGIFResource(data);
	}

	public GifView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initializeView();
	}

	public GifView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initializeView();
	}

	/**
	 * 初始化控件
	 */
	private void initializeView() {
		if (gifResource != 0) {
			InputStream is = getContext().getResources().openRawResource(gifResource);
			byte[] array = streamToBytes(is);
			mMovie = Movie.decodeByteArray(array, 0, array.length);
			movieStart = 0;
			this.invalidate();
		}
	}

	/**
	 * 设置GIF资源ID
	 * 
	 * @param id
	 */
	public void setGIFResource(int id) {
		this.gifResource = id;
		initializeView();
	}

	/**
	 * 获取资源ID
	 * 
	 * @return
	 */
	public int getGIFResoure() {
		return gifResource;
	}

	/**
	 * 设置GIF资源数据
	 * 
	 * @param bytes
	 */
	public void setGIFResource(byte[] bytes) {
		if (bytes == null || bytes.length <= 0) {
			return;
		}
		mMovie = Movie.decodeByteArray(bytes, 0, bytes.length);
		movieStart = 0;
		this.invalidate();
	}

	/**
	 * 设置GIF资源数据
	 * 
	 * @param is
	 */
	public void setGIFResource(InputStream is) {
		if (is == null) {
			return;
		}
		byte[] array = streamToBytes(is);
		mMovie = Movie.decodeByteArray(array, 0, array.length);
		movieStart = 0;
		this.invalidate();
	}

	/**
	 * 设置GIF资源图片
	 * 
	 * @param bitmap
	 */
	public void setGIFResource(Bitmap bitmap) {
		if (bitmap == null) {
			return;
		}
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
		byte[] bitmapdata = bos.toByteArray();
		mMovie = Movie.decodeByteArray(bitmapdata, 0, bitmapdata.length);
		movieStart = 0;
		this.invalidate();
	}

	private static byte[] streamToBytes(InputStream is) {
		ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
		byte[] buffer = new byte[1024];
		int len;
		try {
			while ((len = is.read(buffer)) >= 0) {
				os.write(buffer, 0, len);
			}
		} catch (java.io.IOException e) {
		}
		return os.toByteArray();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		canvas.drawColor(Color.TRANSPARENT);
		super.onDraw(canvas);
		long now = android.os.SystemClock.uptimeMillis();
		if (movieStart == 0) {
			movieStart = now;
		}
		int d = mMovie.duration();
		if (d <= 0) {
			d = 1500;
		}
		if (mMovie != null) {
			int relTime = (int) ((now - movieStart) % d);
			mMovie.setTime(relTime);
			mMovie.draw(canvas, (getWidth() - mMovie.width()) / 2, (getHeight() - mMovie.height()) / 2);
			this.invalidate();
		}

	}
}


特别注意:


使用movie时,你需要把这个控件的硬件加速关闭,否则同样无法播放。貌似硬件加速的属性是3.0以后才有的。
方法是:
gifView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: