ImageLoader在github上的地址:https://github.com/nostra13/Android-Universal-Image-Loader
配置:
1.在app的buildgradle中添加以来:
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
例如:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'org.greenrobot:greendao:3.2.2'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
}
2.重写application,并初始化配置
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); initImageLoader(); } public void initImageLoader(){ ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(getApplicationContext()); config.threadPriority(Thread.NORM_PRIORITY - 2); config.denyCacheImageMultipleSizesInMemory(); config.diskCacheFileNameGenerator(new Md5FileNameGenerator()); config.diskCacheSize(50*1024*1024); config.tasksProcessingOrder(QueueProcessingType.LIFO); config.writeDebugLogs(); ImageLoader.getInstance().init(config.build()); } }
3.配置AndroidManifest
将application标签的name熟悉设置为.MyApplication; 添加权限
android: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">
4.应用实例:
public class MyImageListActivity extends Activity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fr_image_list); ListView listView = findViewById(R.id.listview); listView.setAdapter(new ImageAdapter()); listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView> adapterView, View view, int i, long l) { Log.i("test","click "+i); } }); } @Override protected void onDestroy() { //回收缓存在内存中的图片 ImageLoader.getInstance().clearMemoryCache(); super.onDestroy(); } private class ImageAdapter extends BaseAdapter{ private String[] IMAGES_URI = new String[]{ "http://mowin.org/wp-content/uploads/2017/07/arborists-forestry.jpg", "http://s1.1zoom.me/big0/506/Sky_Grasslands_Rainbow_502473.jpg", }; // 创建DisplayImageOptions对象 private DisplayImageOptions options; public ImageAdapter() { options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.dog) // 下载期间显示的图片 .showImageForEmptyUri(R.drawable.dog)//Uri为空或是错误的时候显示的图片 .showImageOnFail(R.drawable.ic_launcher_background)//发生错误显示的图片 .cacheInMemory(true)//载的图片是否缓存在内存中 .cacheOnDisk(true)//载的图片是否缓存在sdcard中 .considerExifParams(true) .displayer(new CircleBitmapDisplayer(Color.WHITE,5))//图片 .build(); } @Override public int getCount() { return IMAGES_URI.length; } @Override public Object getItem(int i) { return IMAGES_URI[i]; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder; if(view == null){ holder = new ViewHolder(); view = LayoutInflater.from(MyImageListActivity.this).inflate(R.layout.item_list_image,null); holder.imageView = (ImageView)view.findViewById(R.id.item_image); holder.textView = (TextView)view.findViewById(R.id.item_name); view.setTag(holder); }else{ holder = (ViewHolder)view.getTag(); } holder.textView.setText("Item " + (i + 1)); // ImageLoader.getInstance().displayImage(IMAGES_URI[i],holder.imageView,options); return view; } } static class ViewHolder{ ImageView imageView; TextView textView; } }
可以使用的URI:
"http://site.com/image.png" // from Web "file:///mnt/sdcard/image.png" // from SD card "file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail) "content://media/external/images/media/13" // from content provider "content://media/external/video/media/13" // from content provider (video thumbnail) "assets://image.png" // from assets "drawable://" + R.drawable.img // from drawables (non-9patch images)
设置图片的显示方式
displayer(BitmapDisplayer displayer)
SimpleBitmapDisplayer() //默认 正常显示一张图片
CircleBitmapDisplayer(Color.WHITE,5)//圆形图片
RoundedBitmapDisplayer(20) //圆角图片,弧度20. 不推荐用
FadeInBitmapDisplayer(100)//加载图片后渐入的时间
FakeBitmapDisplayer()
图片压缩质量参数
bitmapConfig(Bitmap.Config.RGB_565)
在android中的大图片一般都要经过压缩才能显示,不然容易发生oom,影响一个图片占用空间的有尺寸大小 色彩细节。
public static final Bitmap.Config ALPHA_8 public static final Bitmap.Config ARGB_4444 public static final Bitmap.Config ARGB_8888 public static final Bitmap.Config RGB_565
ARGB指的是一种色彩模式,A代表Alpha,R表示red,G表示green,B表示blue。所有的可见色都是由红绿蓝组成的,所以红绿蓝又称为三原色
ALPHA_8就是Alpha由8位组成--代表8位Alpha位图
ARGB_4444就是由4个4位组成即16位--代表16位ARGB位图
ARGB_8888就是由4个8位组成即32位--代表32位ARGB位图
RGB_565就是R为5位,G为6位,B为5位共16位--代表16位RGB位图
位图位数越高代表其可以存储的颜色信息越多,当然图像也就越逼真。
bitmapConfig(Bitmap.Config.RGB_565) //使用RGB_565会比使用ARGB_8888少消耗2倍的内存
imageScaleType(ImageScaleType.IN_SAMPLE_INT) 或 imageScaleType(ImageScaleType.EXACTLY)
避免使用new RoundedBitmapDisplayer(int) //会创建新的ARGB_8888格式的Bitmap对象;