自定义控件 GifImageView 可以播放gif动态图的ImageView

来源:互联网 发布:曼哈顿计划 知乎 编辑:程序博客网 时间:2024/06/10 03:21

GifImageView支持ImageView控件原生的所有功能,同时还能播放GIF图片。

1.values下新建一个attrs.xml文件,内容如下:

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

    <declare-styleable name="GifImageView">
        <attr name="auto_play" format="boolean"></attr>
    </declare-styleable>

</resources>

2.GifImageView类的编写:

package com.example.gifimageview;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class GifImageView extends ImageView implements OnClickListener {

 private Movie mMovie;
 private long mMovieStart;//记录动画开始的时间
 private int mImageWidth;//GIF图片的宽
 private int mImageHeight;//GIF图片的高

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

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

 public GifImageView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GifImageView);
  int resourceId = getResourceId(a, context, attrs);
  if (resourceId != 0) {
   // 当资源id不等于0时,就去获取该资源的流
   InputStream is = getResources().openRawResource(resourceId);
   // 使用Movie类对流进行解码
   mMovie = Movie.decodeStream(is);
   if (mMovie != null) {
    /// 如果返回值不等于null,就说明这是一个GIF图片,下面获取是否自动播放的属性
    Bitmap bitmap = BitmapFactory.decodeStream(is);
    mImageWidth = bitmap.getWidth();
    mImageHeight = bitmap.getHeight();
    bitmap.recycle();
   }
  }
 }

 @Override
 public void onClick(View v) {
  invalidate();
 }

 @Override
 protected void onDraw(Canvas canvas) {
  if (mMovie == null) {
   // mMovie等于null,说明是张普通的图片,则直接调用父类的onDraw()方法
   super.onDraw(canvas);
  } else {
   // mMovie不等于null,说明是张GIF图片
   playMovie(canvas);
   invalidate();
  }
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  if (mMovie != null) {
   // 如果是GIF图片则重写设定GifImageView的大小
   setMeasuredDimension(mImageWidth, mImageHeight);
  }
 }

 /**
  * 开始播放GIF动画,播放完成返回true,未完成返回false。
  * 播放完成返回true,未完成返回false。
  */
 private boolean playMovie(Canvas canvas) {
  long now = SystemClock.uptimeMillis();
  if (mMovieStart == 0) {
   mMovieStart = now;
  }
  int duration = mMovie.duration();
  if (duration == 0) {
   duration = 1000;
  }
  int relTime = (int) ((now - mMovieStart) % duration);
  mMovie.setTime(relTime);
  mMovie.draw(canvas, 0, 0);
  if ((now - mMovieStart) >= duration) {
   mMovieStart = 0;
   return true;
  }
  return false;
 }

 /**
  * 通过Java反射,获取到src指定图片资源所对应的id。
  * 返回布局文件中指定图片资源所对应的id,没有指定任何图片资源就返回0。
  */
 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;
 }
}

3.MainActivity类,其实什么都没有写:

package com.example.gifimageview;

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

public class MainActivity extends Activity {

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

}

4.布局文件的编写,process_small就是一张gif图片:

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

    <com.example.gifimageview.GifImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/process_small"/>
 
</LinearLayout>

5.AndroidManifest.xml文件,添加android:hardwareAccelerated,防止一部分4.0以上系统的手机启动了硬件加速功能,导致GIF动画无法播放。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gifimageview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:hardwareAccelerated="false" >
        <activity
            android:name="com.example.gifimageview.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


 

2 0