android中播放gif动画之二

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

在上一篇,是使用代码通过构造方法传入要播放的gif动画的id进行获取的。本文进一步改造,让GifView和ImageView一样可以在布局文件中设置src属性,并在GifView中我们使用反射动态获取src的图片资源ID,然后对它进行播放。这样使用起来就灵活多了。代码如下:

GifView:

package com.home.gifview;

import java.io.InputStream;
import java.lang.reflect.Field;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ImageView;

public class GifView extends ImageView {

	private Movie movie;
	private long movieStart;
	private int imageWidth;
	private int imageHeight;

	public GifView(Context context) {
		super(context);
	}

	public GifView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public GifView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.GifView);
		int resourceId = getResourceId(a, context, attrs);
		if (resourceId != 0) {
			InputStream is = getResources().openRawResource(resourceId);
			movie = Movie.decodeStream(is);
			if (movie != null) {
				Bitmap bitmap = BitmapFactory.decodeStream(is);
				imageWidth = bitmap.getWidth();
				imageHeight = bitmap.getHeight();
				bitmap.recycle();
			}
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (movie == null) {
			// 普通图片则直接调用父类的onDraw()方法
			super.onDraw(canvas);
		} else {
			playGif(canvas);
			invalidate();
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		if (movie != null) {
			// 如果是gif图片则定制GifView的大小
			setMeasuredDimension(imageWidth, imageHeight);
		}
	}

	/**
	 * 播放gif动画
	 * 
	 * @param canvas
	 * @return
	 */
	private boolean playGif(Canvas canvas) {
		long now = SystemClock.uptimeMillis();
		if (movieStart == 0) {
			movieStart = now;
		}
		int duration = movie.duration();
		if (duration == 0) {
			duration = 1000;
		}
		int relTime = (int) ((now - movieStart) % duration);
		movie.setTime(relTime);
		movie.draw(canvas, 0, 0);
		if ((now - movieStart) >= duration) {
			movieStart = 0;
			return true;
		}
		return false;
	}

	/**
	 * 使用反射,获取到src指定图片资源所对应的id。
	 * 
	 * @param a
	 * @param context
	 * @param attrs
	 * @return
	 */
	private int getResourceId(TypedArray a, Context context, AttributeSet attrs) {
		try {
			Field field = TypedArray.class.getDeclaredField("mValue");
			field.setAccessible(true);
			TypedValue typedValueObject = (TypedValue) field.get(a);
			return typedValueObject.resourceId;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (a != null) {
				a.recycle();
			}
		}
		return 0;
	}
}

attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="GifView"></declare-styleable>

</resources>

main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.home.gifview.GifView
        android:id="@+id/main_gifView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/loading" />

</RelativeLayout>

禁用硬件加速:

android:hardwareAccelerated="false"





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值