Android_ImageLoader

来源:互联网 发布:手机动漫制作软件 编辑:程序博客网 时间:2024/06/05 11:13
第一步:下载Jar包  Gradle里面      depencies标签下面    加上下面这句话
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
第二步:加权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
application  里加入  android:name=".App"  //.App是第三步中创建的类名
第三步:创建一个类继承 Application
/** * on 2017-09-05. * 这个是系统全局的Application, 它就是系统生命周期最长的上下文; * 系统已启动,并不是直接进入MainActivity;而是先进入Application; * 重写他的目的是,初始化系统全局的一些参数;比如imageLoader的全局配置; * 生命周期和app的生命周期一样, * */
public class App  extends Application{    @Override    public void onCreate() {        super.onCreate();        ImageLoaderConfiguration config = new ImageLoaderConfiguration                .Builder(this)                // max width, max height,即保存的每个缓存文件的最大长宽                .memoryCacheExtraOptions(480, 800)                //线程池内加载的数量                .threadPoolSize(5)                .threadPriority(Thread.NORM_PRIORITY - 2)                .denyCacheImageMultipleSizesInMemory()                // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现                .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))                .memoryCacheSize(2 * 1024 * 1024)                .tasksProcessingOrder(QueueProcessingType.LIFO)                .defaultDisplayImageOptions(DisplayImageOptions.createSimple())                // connectTimeout (5 s), readTimeout (30 s)超时时间                .imageDownloader(new BaseImageDownloader(this, 5 * 1000, 30 * 1000))                // Remove for release app                .writeDebugLogs()                //开始构建                .build();        //初始化imageloader;        ImageLoader.getInstance().init(config);    }}
第四步:Oncreate里面
//获取id
protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);
    //获取id    ImageView imageView = (ImageView) findViewById(R.id.imageView);    //得到imageLoader的实例;    ImageLoader imageLoader = ImageLoader.getInstance();    DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()            //设置图片在下载期间显示的图片            .showImageOnLoading(R.mipmap.ic_launcher)            //设置图片Uri为空或是错误的时候显示的图片            .showImageForEmptyUri(R.mipmap.ic_launcher)            //设置图片加载/解码过程中错误时候显示的图片            .showImageOnFail(R.mipmap.ic_launcher)            .build();
    //第一个参数图片地址,第二个参数控件id
imageLoader.displayImage(images[2], imageView);}
//如需加入到sd卡加
 .cacheOnDisk(true)   //sd卡
 .cacheInMemory(true) 

原创粉丝点击