Android ImageView播放gif图片

当点击图片的时候,gif图片播放,当没有点击的时候是gif的第一帧,这时是一张图片。

参考:https://blog.csdn.net/guolin_blog/article/details/11100315

首先在values目录下新建一个attrs.xml的文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="AnimaImageView">
        <attr name="auto_play" format="boolean">
        </attr>
    </declare-styleable>
</resources>
 

AnimaImageView继承自ImageView,这个类支持ImageView的所有功能。

package com.example.administrator.taiyang;

/**
 * Created by Administrator on 2018/11/4.
 */

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.Build;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;

import java.io.InputStream;
import java.lang.reflect.Field;

/**
 * @author EX_YINQINGYANG
 * @version [Android PABank C01, @2016-09-29]
 * @date 2016-09-29
 * @description 可以播放gif动画的ImageView
 */
public class AnimaImageView extends ImageView implements View.OnClickListener {
    /**
     * 是否自动播放
     */
    private boolean isAutoPlay;
    /**
     * 播放GIF动画的关键类
     */
    private Movie mMovie;
    /**
     * gif宽高
     */
    private BitmapSize bitmapSize;
    /**
     * 播放按钮
     */
    private Bitmap mStartBotton;
    /**
     * 是否正在播放gif
     */
    private boolean isPlaying;
    /**
     * gif开始时间
     */
    private long mMovieStart;

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

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

    public AnimaImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            this.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        }
        obtainStyledAttr(context,attrs,defStyleAttr);
    }

    private void obtainStyledAttr(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a=context.getTheme().obtainStyledAttributes(attrs,R.styleable.AnimaImageView,defStyleAttr,0);
        int resId=getIdentifier(a);
        if(resId!=0){
            // 当资源id不等于0时,就去获取该资源的流
            InputStream is=getResources().openRawResource(resId);
            // 使用Movie类对流进行解码
            mMovie=Movie.decodeStream(is);
            //mMovie不等null说明这是一个GIF图片
            if(mMovie!=null){
                this.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
                //是否自动播放
                isAutoPlay=a.getBoolean(R.styleable.AnimaImageView_auto_play,false);
                /**
                 * 获取gif图片大小
                 */
                Bitmap bitmap= BitmapFactory.decodeStream(is);
                bitmapSize=new BitmapSize(bitmap.getWidth(),bitmap.getHeight());
                bitmap.recycle();
                if(!isAutoPlay){
                    // 当不允许自动播放的时候,得到开始播放按钮的图片,并注册点击事件
                    mStartBotton=BitmapFactory.decodeResource(getResources(),R.drawable.g1);
                    setOnClickListener(this);
                }
            }
        }
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //当时gif图片的时候,控件宽高为gif文件大小
        if(mMovie!=null){
           setMeasuredDimension(bitmapSize.width,bitmapSize.height);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //当为一张普通的图片的时候
        if(mMovie==null){
            super.onDraw(canvas);
        }else{
            //如果自动播放的话,就直接播放
            if(isAutoPlay){
                playMovie(canvas);
                invalidate();
            }else{
                //如果已经点击了播放按钮的话就开始播放gif
                if(isPlaying){
                    if(playMovie(canvas)){
                        isPlaying=false;
                    }
                    invalidate();
                }else{
                    // 还没开始播放就只绘制GIF图片的第一帧,并绘制一个开始按钮
                    mMovie.setTime(0);
                    mMovie.draw(canvas, 0, 0);
                    int offsetW = bitmapSize.width ;
                    int offsetH = bitmapSize.height;
                    canvas.drawBitmap(mStartBotton, offsetW, offsetH, null);
                }
            }
        }
    }
    /**
     * 开始播放GIF动画,播放完成返回true,未完成返回false。
     *
     * @param canvas
     * @return 播放完成返回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);//不断的设置gif的播放位置
        mMovie.draw(canvas, 0, 0);//将movie画在canvas上
        //如果(当前时间-gif开始的时间=gif总时长)说明播放完毕了
        if ((now - mMovieStart) >= duration) {
            mMovieStart = 0;
            return true;
        }
        return false;
    }
    /**
     * 通过反射获取src中的资源id
     * @param a
     */
    private int getIdentifier(TypedArray a) {
        try {
            Field mValueFiled = a.getClass().getDeclaredField("mValue");
            mValueFiled.setAccessible(true);
            TypedValue typedValue= (TypedValue) mValueFiled.get(a);
            return typedValue.resourceId;
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 当点击图片的时候播放gif
     */
    @Override
    public void onClick(View v) {
        isPlaying = true;
        invalidate();
    }

    /**
     * BitmapSize
     */
    class BitmapSize{
        private int width;
        private int height;

        public BitmapSize(int width, int height) {
            this.width = width;
            this.height = height;
        }
    }
}

activity_main.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:attr="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >
 
        <com.example.administrator.taiyang.AnimaImageView
            android:layerType="software"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            attr:auto_play="false"
            android:src="@drawable/g1"
            android:layout_weight= "1"/>
 
</RelativeLayout>
这里要注意包名:<com.example.administrator.taiyang.AnimaImageView

attr:auto_play="false" 是禁止fig动画自动播放。

可改为 attr:auto_play="true" 自动播放。

有些4.0以上系统的手机启动了硬件加速功能之后会导致GIF动画播放不出来,因此我们需要在AndroidManifest.xml中去禁用硬件加速功能。

AndroidManifest.xml,在<application 下增加

 android:hardwareAccelerated="false"
 android:largeHeap="true"

如果src属性里面指定的是一张PNG图片,图片在布局正中央也会显示出来,正是普通ImageView所具备的功能。

猜你喜欢

转载自blog.csdn.net/weixin_39492854/article/details/84225514