zoukankan      html  css  js  c++  java
  • Android 开源框架 ( 十 ) 图片加载框架---Picasso

      Picasso 是Square 公司(SQUARE美国一家移动支付公司)开源的Android 端的图片加载和缓存框架。Square 还开源了Rerefoit 、OkHttp、LeakCanary、Picasso等等都是非常火的开源项目。

      Picasso官方介绍网站:http://square.github.io/picasso/

    一.基本使用

      1.添加依赖

    compile 'com.squareup.picasso:picasso:2.5.2'

      如果需要对加载的图片进行个性化处理可以(转换器Transformation),添加图片处理的依赖库

    compile 'jp.wasabeef:picasso-transformations:2.1.0'
        // If you want to use the GPU Filters
    compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1'

        2.基本使用

        // 加载图片
        Picasso.with(mContext)
                .load(url)
                .placeholder(R.drawable.ic_launcher)
                .error(R.drawable.ic_launcher)
                .into(holder.iv);        

    二.Picasso其他设置

      1.设置的默认图
    .placeholder(R.drawable.default_bg)

      2.调用into的时候明确告诉你没有占位图设置。placeholder和noPlaceholder 不能同时应用在同一个请求上,否则会抛异常。

    .noPlaceholder()

      3.加载图片出错的情况下显示的默认图

    .error(R.drawable.error_iamge)

      4.into 显示到ImageView 都会有一个简单的渐入过度效果,提升用户体验。如果不需要就设置noFade。默认会有渐变.

    .noFade()

      5.设置图片尺寸(Resize)

    .resize(400,200) //该单位是pixels(px)
    .resizeDimen(R.dimen.image_width,R.dimen.image_height) //resizeDimen(int targetWidthResId, int targetHeightResId) //该单位dimen里的属性,可以设置为dp值

      6.设置缩放(Scale)

        只有当原始图片的尺寸大于我们指定的尺寸时,resize才起作用
    .resize(4000,2000)
    .onlyScaleDown()

      7.设置裁剪(Crop)

         7.1 centerCrop()充满ImageView 的边界,居中裁剪。ImageView 的ScaleType 也有这个属性。
     
    .centerCrop()        

        7.2 上面的centerCrop是可能看不到全部图片的,如果你想让View将图片展示完全,可以用centerInside,但是如果图片尺寸小于View尺寸的话,是不能充满View边界的。

     .centerInside()

        7.3 fit 它会自动测量我们的View的大小,然后内部调用reszie方法把图片裁剪到View的大小,这就帮我们做了计算size和调用resize。

      .fit()

     使用fit 还是会出现拉伸扭曲的情况,因此最好配合前面的centerCrop使用

    .fit()
    .centerCrop()

      注意

         1,fit 只对ImageView 有效。
         2,使用fit时,ImageView 宽和高不能为wrap_content,很好理解,因为它要测量宽高。
     
      8.图片旋转rotate(int degree),该方法它是默认以(0,0)点旋转。
    .rotate(180)
     //rotate(float degrees, float pivotX, float pivotY) 以(pivotX, pivotY)为原点旋转
     .rotate(180,100,100)

     9.转换器Transformation 添加依赖时候添加了Picasso Transformation的依赖。

    compile 'jp.wasabeef:picasso-transformations:2.1.0'

       Transformation 这就是Picasso的一个非常强大的功能了,它允许你在load图片 -> into ImageView 中间这个过成对图片做一系列的变换。比如你要做图片高斯模糊、添加圆角、做度灰处理、圆形图片等等都可以通过Transformation来完成。

     具体使用可以自己继续探索,功能很多,
  • 相关阅读:
    SQL 存储过程分页
    SqlServer中代理作业实现总结
    firfox兼容性插件
    C#停靠栏组件 DockPanel Suite
    Web.Config加密
    osql
    prototype.js的Ajax对IE8兼容问题解决方案
    获取系统的字体
    基于RBAC的权限管理系统的实现经典
    c/s(C#)下Ftp的多文件上传及其上传进度
  • 原文地址:https://www.cnblogs.com/bugzone/p/Picasso.html
Copyright © 2011-2022 走看看