1.导包

 implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

2.权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.imageloader2"><uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><applicationandroid:name=".MyApplication"android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

3.初始化

package com.example.imageloader2;import android.app.Application;import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;public class MyApplication extends Application {@Overridepublic void onCreate() {super.onCreate();ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);ImageLoader.getInstance().init(configuration);}
}

4.使用
注意:要先申请存储权限,否则可能出现图片下载失败

package com.example.imageloader2;import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.Arrays;
import java.util.List;public class MainActivity extends AppCompatActivity {private static final String TAG = MainActivity.class.getSimpleName();private GridView mGridView;private ImageAdapter mImageAdapter;private String[] mImages = new String[]{"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg"};private List<String> mImageList;private boolean isGridViewIdle = true;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mImageList = Arrays.asList(mImages);mImageAdapter = new ImageAdapter(this, mImageList);mGridView = (GridView)findViewById(R.id.grid_view);mGridView.setOnScrollListener(new AbsListView.OnScrollListener(){@Overridepublic void onScrollStateChanged(AbsListView absListView, int i) {if(i == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {isGridViewIdle = true;mImageAdapter.notifyDataSetChanged();}else {isGridViewIdle = false;}}@Overridepublic void onScroll(AbsListView absListView, int i, int i1, int i2) {}});mGridView.setAdapter(mImageAdapter);//确认存储权限boolean exit = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;if(!exit) {requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);}}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);boolean permit = true;for(int result : grantResults){if(result == -1) {permit = false;break;}}Log.d(TAG, "permit-:"+permit);if(permit) {mImageAdapter.notifyDataSetChanged();}}class ImageAdapter extends BaseAdapter {private Context mContext;private List mImages;public ImageAdapter(Context mContext, List mImages) {this.mContext = mContext;this.mImages = mImages;}@Overridepublic int getCount() {return mImages.size();}@Overridepublic Object getItem(int i) {return mImages.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic View getView(int i, View view, ViewGroup viewGroup) {ViewHolder holder = null;if(view == null) {view = getLayoutInflater().inflate(R.layout.image_item, null);holder = new ViewHolder();holder.imageView = view.findViewById(R.id.square_item);view.setTag(holder);}else {holder = (ViewHolder) view.getTag();}final String uri = (String) getItem(i);ImageView imageView = holder.imageView;/*final String tag = (String) holder.imageView.getTag();if(uri.equals(tag)) {imageView.setImageResource(R.drawable.ic_launcher_background);}if(isGridViewIdle) {imageView.setTag(uri);ImageLoader.getInstance().displayImage(uri, imageView);}*/if(isGridViewIdle) {ImageLoader.getInstance().displayImage(uri, imageView);}return view;}class ViewHolder {SquareImageView imageView;}}
}

5.布局

  1. activity_main.xml

     <?xml version="1.0" encoding="utf-8"?><LinearLayout 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"tools:context=".MainActivity"android:orientation="vertical"><GridViewandroid:id="@+id/grid_view"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:horizontalSpacing="5dp"android:verticalSpacing="5dp"android:listSelector="@android:color/transparent"android:numColumns="3"android:stretchMode="columnWidth"android:visibility="visible"/></LinearLayout>
    
  2. image_item.xml

     <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"><com.example.imageloader2.SquareImageViewandroid:id="@+id/square_item"android:layout_width="match_parent"android:layout_height="wrap_content"android:scaleType="centerCrop"android:src="@mipmap/ic_launcher"/></LinearLayout>
    

6.自定义宽高相同的ImageView

package com.example.imageloader2;import android.content.Context;
import android.util.AttributeSet;
import androidx.annotation.Nullable;public class SquareImageView extends androidx.appcompat.widget.AppCompatImageView {public SquareImageView(Context context) {super(context);}public SquareImageView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public SquareImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, widthMeasureSpec);}
}

ImageLoader简单使用相关推荐

  1. 【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )

    作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50824912 相关地址介绍 : -- Universal I ...

  2. 如何通过自学成为一名 Android 应用开发工程师?

    作者:陈昱全 链接:https://www.zhihu.com/question/21565227/answer/142800222 来源:知乎 1.初级之路 1.1 项目引路 对于自学的人来说,在边 ...

  3. Android-Universal-Image-Loader源码知识笔记

    ImageLoader简单使用 ImageLoader的二级分包主要由cache.core和utils组成,三者分别负责缓存.核心加载类和工具类. 在核心加载类core的子分包中,包括显示.下载.进度 ...

  4. Android ImageLoader(Android-Universal-Image-Loader)【1】概述及使用简单介绍

     Android ImageLoader(Android-Universal-Image-Loader)[1]概述及使用简单介绍 一,前言:为什么要引入Android-Universal-Imag ...

  5. 实现简单的ImageLoader

    Android开发中经常会对图片进行处理,图片如何加载和缓存的实现有难有易,这里实现一个简单的ImageLoader,只是简述其中的基本原理. 整个实现需要用到图片的缓存和加载,首先需要实现图片的缓存 ...

  6. Android自定义简单的图片加载器(ImageLoader)

    废话不多述,首先来说明下 为什么要用图片加载器 呢,就是为了避免图片重复从网络加载.也就是在第一次从网络加载之后就把图片缓存在本地,下次用的时候直接从本地查找,有的话就直接用,没有再从网络加载. 加载 ...

  7. Android -- ImageLoader本地缓存

    传送门 <Android -- ImageLoader简析>  http://www.cnblogs.com/yydcdut/p/4008097.html 本地缓存 在缓存文件时对文件名称 ...

  8. android使用ImageLoader实现图片缓存(安卓开发必备)

    相信大家在学习以及实际开发中基本都会与网络数据打交道,而这其中一个非常影响用户体验的就是图片的缓存了,若是没有弄好图片缓存,用户体验会大大下降,总会出现卡顿情况,而这个问题尤其容易出现在ListVie ...

  9. android Glide简单使用

    今天,简单讲讲Android里Glide的简单使用. Android框架系列: 一.android EventBus的简单使用 二.android Glide简单使用 对于Glide这个加载图片的框架 ...

最新文章

  1. 解决SpringBoot更新数据到MySQL乱码问题
  2. 如何用最傻的办法查看tensorflow-yolov3模型测试VOC数据集时预测值(图框)与真实值(图框)的对比效果?
  3. future 线程报错后_线程池运用实例——一次错误的多线程程序设计以及修复过程...
  4. 单位根反演[loj6485]LJJ 学二项式定理
  5. pandas使用get_dummies进行one-hot编码
  6. rpm包安装mysql数据库
  7. MySQL的reset master
  8. oracle集群数据库事务配置,在 .NET中使用Oracle数据库事务-安装配置-Oracle频道-中国IT实验室...
  9. .net网页input 赋值 提交时空白_《快速掌握PyQt5》第三十章 网页交互QWebEngineView...
  10. weibo.cn html5,微博爬虫:爬取微博正文、关注人
  11. 学习React基本渲染数据操作(-)
  12. c 语言小游戏源程序,c/c++小游戏源代码
  13. ArcGIS:如何创建地理数据库、创建要素类数据集、导入要素类、表?
  14. 游戏圈子--创业股份没拿到,你该怨谁?
  15. Pyhon 登陆邮箱
  16. DIY,PC采购,服务器介绍
  17. 【光模块、光接口及光纤知识】
  18. 单字母密码密码分析(上)
  19. 提高信息系统的安全防护能力,一篇文章带你了解等保测评的重要性
  20. Seq2Seq模型中的贪心搜索(Greedy Search)

热门文章

  1. 捋一捋Python中的数学运算math库(上篇)
  2. java课设 学生信息管理系统
  3. OCaml文档(一) - 核心语言部分
  4. 这几个步骤,让你的电脑避免卡顿~
  5. 三星BUDS+如何连接IPAD
  6. 疯狂python讲义epub,疯狂英语阅读版CR201510.pdf
  7. vulkan_笔记1
  8. 彩虹六号如何修改联结服务器,彩虹六号:围攻 修改匹配区域教程 更改匹配服务器...
  9. 真正的JavaScript加密和网页资源加密系统,从原理到实现
  10. [源码和文档分享]基于Win32 API实现的中国象棋游戏