转载请注明:
http://blog.csdn.net/u012975705/article/details/48717391
在看这篇博文之前对attr自定义属性还是不是很熟的童鞋可以先看看:Android中attr详解(http://blog.csdn.net/u012975705/article/details/48716797)
源码下载:https://github.com/noyo/GifTest
效果图
显示的事两张动态gif图
实现代码
1、布局activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:gif="http://schemas.android.com/apk/res-auto"
android:gravity="center"
android:orientation="vertical" >
<com.practice.noyet.giftest.MyGiftView
android:id="@+id/gif1"
gif:gif="@raw/gift1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:enabled="false" />
<com.practice.noyet.giftest.MyGiftView
android:layout_marginTop="10dp"
gif:gif="@raw/gift2"
android:id="@+id/gif2"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:enabled="false" />
</LinearLayout>
2、自定义的com.practice.noyet.giftest.MyGiftView.java代码(代码中都有注释,我就不废话了):
package com.practice.noyet.giftest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import j