Android开发中加载Gif动画图片方法

Gif动画,我使用了三种方式

1.自定义控件加载gif动画

①.首先自定义一个GifView,用于显示Gif图片,具体代码如下:

public class GifView extends View {
    private Resources resources;
    private Movie mMovie;
    private long mMovieStart;
    private float ratioWidth;
    private float ratioHeight;
    public GifView(Context context) {
        this(context,null);
    }
 
    public GifView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
 
    public GifView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        resources = context.getResources();
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.GifView);
        int resourceId = ta.getResourceId(R.styleable.GifView_src, -1);
        setGifResource(resourceId);
        ta.recycle();
    }
 
    public void setGifResource(int resourceId) {
        if (resourceId==-1){
            return;
        }
        InputStream is = resources.openRawResource(resourceId);
        mMovie = Movie.decodeStream(is);
        requestLayout();
    }
    public  void setGifStream(InputStream is){
        mMovie = Movie.decodeStream(is);
        requestLayout();
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mMovie!=null){
            int w = mMovie.width();
            int h = mMovie.height();
            if (w<=0){
                w=1;
            }
            if (h<=0){
                h=1;
            }
            int pLeft = getPaddingLeft();
            int pRight = getPaddingRight();
            int pTop = getPaddingTop();
            int pBottom = getPaddingBottom();
            int widthSize;
            int heightSize;
            w+=pLeft+pRight;
            h+=pTop+pBottom;
            w=Math.max(w,getSuggestedMinimumWidth());
            h=Math.max(h,getSuggestedMinimumHeight());
            widthSize= resolveSizeAndState(w,widthMeasureSpec,0);
            heightSize= resolveSizeAndState(h,heightMeasureSpec,0);
            ratioWidth = (float) widthSize/w;
            ratioHeight = (float) heightSize/h;
            setMeasuredDimension(widthSize,heightSize);
        }else{
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        long now = SystemClock.uptimeMillis();
        if (mMovieStart ==0){ //第一次进入
            mMovieStart =now;
        }
        if (mMovie!=null){
            int dur = mMovie.duration();
            if (dur==0){
                dur=1000;
            }
            int relTime= (int) ((now-mMovieStart)%dur);
            mMovie.setTime(relTime);
           //  mMovie.draw(canvas,0,0);
            float scale=Math.min(ratioWidth,ratioHeight);
            canvas.scale(scale,scale);
            mMovie.draw(canvas,0,0);
            invalidate();
        }
    }
}

②.在布局文件中添加自定义的控件,我这加了两个,第一个展示assets文件中的gif图片,一个展示drawable中的gif图片

 
    

③.展示gif图片,具体代码如下:

         //方法一 使用自定义gifview播放gif图片
        mGvLocalPhoto = (GifView) findViewById(R.id.gv_local_photo);
        mGvPhoto = (GifView) findViewById(R.id.gv_photo);
      
        try {
            InputStream is = getAssets().open("assetphoto.gif");
            mGvPhoto.setGifStream(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

2.使用Glide加载gif动画

Glide 地址: https://github.com/bumptech/glide

①.首先配置build.gradle

 compile 'com.github.bumptech.glide:glide:3.7.0'

②.配置好Glide之后,然后通过基本的用法去加载,加载代码如下:

// 使用Glide播放gif图片
 mIvPhoto = (ImageView) findViewById(R.id.iv_photo);
 Glide.with(this)
          .load(R.drawable.gifphoto)
          .asGif()
          .diskCacheStrategy(DiskCacheStrategy.SOURCE)
          .into(mIvPhoto);

注意:diskCacheStrategy是为其添加缓存策略,其中缓存策略可以为:Source及None,None及为不缓存,Source缓存原型。

3.使用android-gif-drawable加载gif动画

android-gif-drawable 地址:https://github.com/koral--/android-gif-drawable

①.首先配置build.gradle

compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.7'

②.在xml中添加GifImageView控件


③.调用android-gif-drawable

 //使用android-gif-drawable 库
        GifImageView mGifIvPhoto = (GifImageView) findViewById(R.id.giv_photo);
        try {
            //加载asset文件中的gif图片
            GifDrawable gif = new GifDrawable(getAssets(), "assetphoto.gif");
            mGifIvPhoto.setImageDrawable(gif);
        } catch (IOException e) {
            e.printStackTrace();
        }

你可能感兴趣的:(android,动画,kotlin)