Android中加載Gif動(dòng)畫

Gif動(dòng)畫,我使用了三種方式

1.自定義控件加載gif動(dòng)畫

①.首先自定義一個(gè)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){ //第一次進(jìn)入
            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();
        }
    }
}

②.在布局文件中添加自定義的控件,我這加了兩個(gè),第一個(gè)展示assets文件中的gif圖片,一個(gè)展示drawable中的gif圖片

 <com.example.wen.adroid.view.GifView
        android:id="@+id/gv_photo"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:layout_height="0dp"
       />
    <com.example.wen.adroid.view.GifView
        android:id="@+id/gv_local_photo"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:layout_height="0dp"
        app:src="@drawable/gifphoto"/>

③.展示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動(dòng)畫

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動(dòng)畫

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

①.首先配置build.gradle

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

②.在xml中添加GifImageView控件

<pl.droidsonroids.gif.GifImageView
        android:id="@+id/giv_photo"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:src="@drawable/gifphoto"
        />

③.調(diào)用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();
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容