Glide和Picasso

区别:

   Gilede

Glide可以根据Imageview的大小自己决定图片大小

多样化媒体加载

Glide 不仅是一个图片缓存,它支持 Gif、WebP、缩略图。甚至是 Video

生命周期集成

通过设置绑定生命周期,我们可以更加高效的使用Glide提供的方式进行绑定,这样可以更好的让加载图片的请求的生命周期动态管理起来

高效的缓存策略

A. 支持Memory和Disk图片缓存

B. Picasso 只会缓存原始尺寸的图片,而 Glide 缓存的是多种规格,也就意味着 Glide 会根据你 ImageView 的大小来缓存相应大小的图片尺寸

C. 内存开销小默认的 Bitmap 格式是 RGB_565 格式,而 Picasso 默认的是 ARGB_8888 格式,这个内存开销要小一半。

使用方法复杂由于Glide其功能强大,所以使用的方法非常多,其源码也相对的复杂  包较大

相比其他图片加载库(Picasso & Fresco)对比Picasso Glide 是在Picasso 基础之上进行的二次开发做了不少改进,不过这也导致包比 Picasso 大不少,不过也就不到 500k(Picasso 是100多k),用法较为复杂,不过毕竟级别还是蛮小的,影响不是很大对比Fresco使用较Fresco简单,但性能(加载速度 & 缓存)却比不上Fresco

Picasso

ImageView targetImageView = (ImageView) findViewById(R.id.ImageView);

        String Url = "http://218.192.170.132/1.jpg"

//Picasso使用了流式接口的调用方式

//Picasso类是核心实现类。

//实现图片加载功能至少需要三个参数: Picasso

//with(Context context)

//Context对于很多Android API的调用都是必须的,这里就不多说了 .with(context)

//load(String imageUrl):被加载图像的Url地址。

//大多情况下,一个字符串代表一个网络图片的URL。  .load(Url)

//into(ImageView targetImageView):图片最终要展示的地方。

.into(targetImageView);

2.2.2 图片转换使用最少的内存完成复杂的图片转换,转换图片以适合所显示的ImageView,来减少内存消耗Picasso.with(context) .load(url)

//裁剪图片尺寸 .resize(50, 50)

//设置图片圆角 .centerCrop() .into(imageView)2.2.3 加载过重 & 错误处理Picasso支持加载过程中和加载错误时显示对应图片Picasso.with(context) .load(url)

//加载过程中的图片显示 .placeholder(R.drawable.user_placeholder)//加载失败中的图片显示

//如果重试3次(下载源代码可以根据需要修改)还是无法成功加载图片,则用错误占位符图片显示。

.error(R.drawable.user_placeholder_error)

.into(imageView);

2.2.4 在Adapter中的回收不在视野的ImageView和取消已经回收的ImageView下载进程

@Override

public void getView(int position, View convertView, ViewGroup parent) {

                 SquaredImageView view = (SquaredImageView) convertView;

                 if (view == null) {

                                      view = new SquaredImageView(context);

                         }

                String url = getItem(position); Picasso.with(context).load(url).into(view);

}

2.2.5 从不同资源源加载支持多种数据源 网络、本地、资源、Assets 等

//加载资源文件Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);

//加载本地文件Picasso.with(context).load(new File("/images/oprah_bees.gif")).into(imageView2);

Picasso是Android中的一个图片加载开源库

缺点

功能较为简单-图片加载;

性能(加载速度等等)较其他图片加载库(Glide、Fresco)较差自身无实现“本地缓存”

2.2.7

支持优先级处理每次任务调度前会选择优先级高的任务,比如 App 页面中 Banner 的优先级高于 Icon 时就很适用。

2.2.8

支持飞行模式、并发线程数根据网络类型而变手机切换到飞行模式或网络类型变换时会自动调整线程池最大并发数,比如 wifi 最大并发为 4, 4g 为 3,3g 为 2

2.2.9

“无”本地缓存无”本地缓存,不是说没有本地缓存,而是 Picasso 自己没有实现,交给了 Square 的另外一个网络库 okhttp 去实现,这样的好处是可以通过请求 Response Header 中的 Cache-Control 及 Expired 控制图片的过期时间


使用Glide

  Glide
                        .with(MainActivity.this)
                        .load(IMG_PATH)
                        .listener(requestListener)
                        .placeholder(R.mipmap.ic_launcher)
                        .into(imageView);
  requestListener = new RequestListener() {
            @Override
            public boolean onException(Exception e, Object model, Target target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(Object resource, Object model, Target target, boolean isFromMemoryCache, boolean isFirstResource) {
                return false;
            }
        };

Notice:ManiFest.xml加权限啊!!!

Glide库:

  compile 'com.github.bumptech.glide:glide:3.7.0'

Retrofit:

 compile 'com.squareup.retrofit2:retrofit:2.1.0'
 compile 'com.squareup.retrofit2:converter-gson:2.1.0'
 compile 'com.squareup.retrofit2:converter-scalars:2.1.0'

RxJava:

 compile 'io.reactivex:rxandroid:1.2.1'
 compile 'io.reactivex:rxjava:1.1.6'

BitmapFactory....;

内存缓存技术:LrucCache(V4包中),这个类适合做缓存图片,它的主要算法原理就是把最近使用的对象用强引用存储在LinkedHashMap中,并且把最少使用的对象在缓存值达到预设定值之前从内存中移除。

   

private LruCache<String, Bitmap> mMemoryCache;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
	// 获取到可用内存的最大值,使用内存超出这个值会引起OutOfMemory异常。
	// LruCache通过构造函数传入缓存值,以KB为单位。
	int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
	// 使用最大可用内存值的1/8作为缓存的大小。
	int cacheSize = maxMemory / 8;
	mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
		@Override
		protected int sizeOf(String key, Bitmap bitmap) {
			// 重写此方法来衡量每张图片的大小,默认返回图片数量。
			return bitmap.getByteCount() / 1024;
		}
	};
}
 
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
	if (getBitmapFromMemCache(key) == null) {
		mMemoryCache.put(key, bitmap);
	}
}
 
public Bitmap getBitmapFromMemCache(String key) {
	return mMemoryCache.get(key);
}
在这个例子当中,使用了系统分配给应用程序的八分之一内存来作为缓存大小。在中高配置的手机当中,这大概会有4兆(32/8)的缓存空间。一个全屏幕的 GridView 使用4张 800x480分辨率的图片来填充,则大概会占用1.5兆的空间(800*480*4)。因此,这个缓存大小可以存储2.5页的图片。

当向 ImageView 中加载一张图片时,首先会在 LruCache 的缓存中进行检查。如果找到了相应的键值,则会立刻更新ImageView ,否则开启一个后台线程来加载这张图片

public void loadBitmap(int resId, ImageView imageView) {
	final String imageKey = String.valueOf(resId);
	final Bitmap bitmap = getBitmapFromMemCache(imageKey);
	if (bitmap != null) {
		imageView.setImageBitmap(bitmap);
	} else {
		imageView.setImageResource(R.drawable.image_placeholder);
		BitmapWorkerTask task = new BitmapWorkerTask(imageView);
		task.execute(resId);
	}

BitmapWorkerTask 还要把新加载的图片的键值对放到缓存中:

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
	// 在后台加载图片。
	@Override
	protected Bitmap doInBackground(Integer... params) {
		final Bitmap bitmap = decodeSampledBitmapFromResource(
				getResources(), params[0], 100, 100);
		addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
		return bitmap;
	}

    

猜你喜欢

转载自blog.csdn.net/Enbir/article/details/80921555