Android图片加载框架Picasso使用教程 (三)

时间:2022-06-26 15:30:53

前面我们对Picasso的用法有了一定得了解,下面就分析一下一些特殊情况下,Picasso的用法.


调用.noFade()

  Picasso的默认图片加载方式有一个淡入的效果,如果调用了noFade(),加载的图片将直接显示在ImageView上


  1. <font color="rgb(85, 85, 85)">Picasso
  2.     .with(context)
  3.     .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  4.     .placeholder(R.mipmap.ic_launcher)  
  5.     .error(R.mipmap.future_studio_launcher)  
  6.     .noFade()
  7.     .into(imageViewFade);</font>
复制代码

调用.noPlaceholder()

  有一个场景,当你从网上加载了一张图片到Imageview上,过了一段时间,想在同一个ImageView上展示另一张图片,这个时候你就会去调用Picasso,进行二次请求,这时Picasso就会把之前的图片进行清除,可能展示的是.placeholder()的图片,给用户并不是很好的体验,如果调用了noPlaceholder(),就不会出现这种情况.


  1. <font color="rgb(85, 85, 85)">Picasso
  2.     .with(context)
  3.     .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  4.     .placeholder(R.mipmap.ic_launcher)  
  5.     .into(imageViewNoPlaceholder, new Callback() {
  6.         @Override
  7.         public void onSuccess() {
  8.             // 当上次加载完成后,进行二次加载
  9.             Picasso
  10.                 .with(context)
  11.                .load(UsageExampleListViewAdapter.eatFoodyImages[1])
  12.                .noPlaceholder()
  13.                .into(imageViewNoPlaceholder);
  14.         }

  15.         @Override
  16.         public void onError() {

  17.         }
  18.     });</font>
复制代码

调用resize(x, y)来自定义图片的加载大小

如果图片很大或者想自定义图片的显示样式,可以调用该API来解决这个问题;


  1. <font color="rgb(85, 85, 85)">Picasso
  2.     .with(context)
  3.     .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  4.     .resize(600, 200)  
  5.     .into(imageViewResize);</font>
复制代码


调用`onlyScaleDown()来缩短图片的加载计算时间

如果我们调用了resize(x,y)方法的话,Picasso一般会重新计算以改变图片的加载质量,比如一张小图变成一张大图进行展示的时候,但是如果我们的原图是比我们从新resize的新图规格大的时候,我们就可以调用onlyScaleDown()来直接进行展示而不再重新计算.


  1. <font color="rgb(85, 85, 85)">Picasso
  2.     .with(context)
  3.     .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  4.     .resize(6000, 2000)
  5.     .onlyScaleDown() // 如果图片规格大于6000*2000,将只会被resize
  6.     .into(imageViewResizeScaleDown);</font>
复制代码

对拉伸图片的处理

如果图片被操作了,可能在展示的时候就会比较丑,我们是想改变这种情景的,Picasso给我们提供了两种选择进行图片展示,centerCrop() 或者centerInside().

  • centerCrop() - 图片会被剪切,但是图片质量看着没有什么区别
  • Inside()- 图片会被完整的展示,可能图片不会填充满ImageView`,也有可能会被拉伸或者挤压

  1. <font color="rgb(85, 85, 85)">Picasso
  2.     .with(context)
  3.     .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  4.     .resize(600, 200)
  5.     .centerInside() 或者调用 .centerCrop()
  6.     .into(imageViewResizeCenterInside);</font>
复制代码


调用.fit()来智能展示图片

如果调用了该API, Picasso会对图片的大小及ImageView进行测量,计算出最佳的大小及最佳的图片质量来进行图片展示,减少内存,并对视图没有影响;


  1. <font color="rgb(85, 85, 85)">Picasso
  2.     .with(context)
  3.     .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  4.     .fit()
  5.     .into(imageViewHero);</font>
复制代码


调用.priority()设置图片加载的优先级

如果一个屏幕上顶部图片较大,而底部图片较小,因为Picasso是异步加载,所以小图会先加载出来,但是对于用户来说,更希望看到的是上面的图片先加载,底部的图片后加载,Picasso支持设置优先级,分为HIGH, MEDIUM, 和 LOW,所有的加载默认优先级为MEDIUM;


  1. <font color="rgb(85, 85, 85)">Picasso
  2.     .with(context)
  3.     .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  4.     .fit()
  5.     .priority(Picasso.Priority.HIGH)
  6.     .into(imageViewHero);</font>
复制代码


调用tag()为请求添加标记提升用户体验

  我们都知道,在一个ListView的子item中加载一张图片是很常见的,这些图片都来源于网络请求,如果这个listview有上千条数据,当用户快速滑动的时候,每个item会不断的被复用,当然Picasso的请求也不断地进行请求,取消请求,再次请求,再次取消的操作(对屏幕外的自动取消请求),但是如果有一个方案,可以在用户在快速滑动的时候全部停止请求,只有在滑动停止时再去请求,就非常完美了;

Picasso提供了三种设置Tag的方式

  • 暂停标记 pauseTag()
  • 可见标记 resumeTag()
  • 取消标记 cancleTag()

pauseTag() 和 resumeTag()的用法

在图片请求时添加标记


  1. <font color="rgb(85, 85, 85)">Picasso
  2.     .with(context)
  3.     .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  4.     .tag("Profile ListView") //参数为 Object
  5.     .into(imageViewWithTag);</font>
复制代码

然后让listview实现滑动监听



  1. <font color="rgb(85, 85, 85)">@Override
  2.   public void onScrollStateChanged(AbsListView view, int scrollState) {
  3.     final Picasso picasso = Picasso.with(context);

  4.     if (scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_TOUCH_SCROLL) {
  5.           picasso.resumeTag("Profile ListView");
  6.     } else {
  7.           picasso.pauseTag("Profile ListView");
  8.     }
  9.   }</font>
复制代码

cancleTag()的使用场景

试想一下,当你在浏览购物车的时候,这个时候就会去展示所有被选中item的图片资源,如果这个时候用户点击了购买按钮,就会弹出一个progressdialog去请求数据以进行页面跳转,这个时候原来的请求就需要取消掉了;


  1. <font color="rgb(85, 85, 85)">public void buyButtonClick(View v) {
  2.      showDiaolg();

  3.     // 取消网络请求
  4.     Picasso
  5.         .with(context)
  6.         .cancelTag("ShoppingCart");
  7. }</font>
复制代码


注意:如果tag状态为pause或者resume的话,Picasso会对tag持有一个引用,如果此时用户退出了当前Activity,垃圾回收机制进行回收的时候,就会出现内存泄露,所以需要在onDestory()方法中进行相应处理;


.fetch() , .get() 及 Target之间的区别
  • .fetch() - 该方法会在后台异步加载一张图片,但是不会展示在ImageView上,也不会返回Bitmap,这个方法只是为了将获取到的资源加载到本地和内存中,为了后期加载缩短时间;

  • .get() - 该方法也是一个异步线程,不过加载完成后会返回一个Bitmap,但是需要注意,该方法不能在主线程中调用,因为会造成线程阻塞;

  • Target - 我们之前调用.into()方法,只是将获取到的资源加载到ImageView中,但我们还可以将资源作为回调放到Target中,上代码:

  1. <font color="rgb(85, 85, 85)">private Target target = new Target() {
  2.     @Override
  3.     public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
  4.         //加载成功后会得到一个bitmap,可以自定义操作
  5.     }

  6.     @Override
  7.     public void onBitmapFailed(Drawable errorDrawable) {
  8.         // 加载失败进行相应处理
  9.     }

  10.     @Override
  11.     public void onPrepareLoad(Drawable placeHolderDrawable) {

  12.     }
  13. };

  14. Picasso
  15.     .with(context)
  16.     .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  17.     .into(target);</font>
复制代码

注意:你可以使用.get()或者Target获取图片的Bitmap,但是当你使用Target时,不能使用匿名内部类的方式,因为垃圾回收机制在你获取不到Bitmap的时候会把对象回收;


Picasso在自定义Notifications上的使用

Picasso有一个功能是可以加载图片到RemoteViews上,而RemoteViews是用在Widgets及自定义notification布局上的,下面通过一个小的示例来看Picasso是如何起作用的;


  1. <font color="rgb(85, 85, 85)"> private void testRemoteView() {
  2.         RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.item_picasso);
  3.         remoteViews.setImageViewResource(R.id.iv_remoteview,R.mipmap.abc);

  4.         remoteViews.setTextViewText(R.id.tv_title,"This Title");
  5.         remoteViews.setTextViewText(R.id.tv_desc,"This desc");

  6.         remoteViews.setTextColor(R.id.tv_title,getResources().getColor(android.R.color.black));
  7.         remoteViews.setTextColor(R.id.tv_desc,getResources().getColor(android.R.color.holo_blue_bright));

  8.         NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
  9.                                                 .setSmallIcon(R.mipmap.notifation)
  10.                                                 .setContentTitle("Context Title")
  11.                                                 .setContentText("Content Text")
  12.                                                 .setContent(remoteViews)
  13.                                                 .setPriority(NotificationCompat.PRIORITY_MIN);

  14.         Notification notification = builder.build();

  15.         if (Build.VERSION.SDK_INT > 16){
  16.             notification.bigContentView = remoteViews;
  17.         }
  18.         NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
  19.         mNotificationManager.notify(NOTIFICATION_ID,notification);

  20.         Picasso.with(MainActivity.this)
  21.                 .load("http://www.jycoder.com/json/Image/3.jpg")
  22.                 .into(remoteViews,R.id.iv_remoteview,NOTIFICATION_ID,notification);
  23.     }</font>
复制代码

上面可以看到,Picasso的使用也是非常简单,只需要调用.into()的另一个重载方法即可:
.into(Android.widget.RemoteViews remoteViews, int viewId, int notificationId, android.app.Notification notification)

效果如下

Android图片加载框架Picasso使用教程 (三)