android顯示網路gif圖片

來源:互聯網
上載者:User

標籤:

這功能源自負責app中要加一個顯示gif廣告圖功能。

android內建控制項不支援gif圖片,網上很多通過擴充ImageView或View來實現支援gif圖片,但在android4.0後,需要關閉硬體加速功能才能使用,而且也容易出現記憶體溢出問題。


網上找了兩個開源包來實現顯示Gif圖

android-gif-drawable 支援gif顯示的view控制項

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

用jni實現的,編譯產生so庫後直接xml定義view,據說效能比較好,也能比較好避免記憶體記憶體溢出問題。

在Android Studio項目添加使用:

build.gradle檔案dependencies新增內容:

dependencies {

    compile ‘pl.droidsonroids.gif:android-gif-drawable:1.1.+‘ /* 添加gif控制項陳列庫引用 */

}


xUtils 

項目地址:https://github.com/wyouflf/xUtils

包含了很多實用的android工具,這裡主要用它下載檔案


MainActivity.java

package com.penngo.gif;import android.app.Activity;import android.content.Context;import android.os.Environment;import android.os.Bundle;import android.util.Log;import com.lidroid.xutils.HttpUtils;import com.lidroid.xutils.exception.HttpException;import com.lidroid.xutils.http.ResponseInfo;import com.lidroid.xutils.http.callback.RequestCallBack;import java.io.File;import pl.droidsonroids.gif.GifDrawable;import pl.droidsonroids.gif.GifImageView;/** * * https://github.com/koral--/android-gif-drawable * https://github.com/wyouflf/xUtils */public class MainActivity extends Activity {    private final String tag = "MainActivity-->";    private GifImageView gif1;    private GifImageView gif2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        gif1 = (GifImageView)this.findViewById(R.id.info_gif1);        gif2 = (GifImageView)this.findViewById(R.id.info_gif2);        initGif();    }    private void initGif(){        String url1 = "http://img5.imgtn.bdimg.com/it/u=3026352344,1511311477&fm=21&gp=0.jpg";        String url2 = "http://img5.imgtn.bdimg.com/it/u=808161139,2623525132&fm=21&gp=0.jpg";        File saveImgPath = this.getImageDir(this);        File gifSavePath1 = new File(saveImgPath, "gif1");        File gifSavePath2 = new File(saveImgPath, "gif2");        displayImage(url1, gifSavePath1, gif1);        displayImage(url2, gifSavePath2, gif2);    }    public void displayImage(String url, File saveFile, final GifImageView gifView){        HttpUtils http = new HttpUtils();        // 下載圖片        http.download(url, saveFile.getAbsolutePath(), new RequestCallBack<File>() {            public void onSuccess(ResponseInfo<File> responseInfo) {                try {                    Log.e(tag, "onSuccess========" + responseInfo.result.getAbsolutePath());                    GifDrawable gifFrom = new GifDrawable( responseInfo.result.getAbsolutePath() );                    gifView.setImageDrawable(gifFrom);                }                catch(Exception e){                    Log.e(tag, e.getMessage());                }            }            public void onFailure(HttpException error, String msg) {                Log.e(tag, "onFailure========" + msg);            }        });    }    public File getFilesDir(Context context, String tag){        if(isSdCardExist() == true){            return context.getExternalFilesDir(tag);        }        else{            return context.getFilesDir();        }    }    public File getImageDir(Context context){        File file = getFilesDir(context, "images");        return file;    }    public boolean isSdCardExist() {        if (Environment.getExternalStorageState().equals(                Environment.MEDIA_MOUNTED)) {            return true;        }        return false;    }}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">    <TextView android:text="@string/label_info"        android:id="@+id/info"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <!-- gif控制項 -->    <pl.droidsonroids.gif.GifImageView        android:id="@+id/info_gif1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:scaleType="fitXY"        android:layout_below="@+id/info"        />    <pl.droidsonroids.gif.GifImageView        android:id="@+id/info_gif2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:scaleType="fitXY"        android:layout_below="@+id/info_gif1"        /></RelativeLayout>

運行效果:


android顯示網路gif圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.