picasso使用

  1. // 举例子   
  2. Picasso.with(this)  
  3.           // 设置图片的路径,可以是网络url,也可以是本地图片路径  
  4.         .load(R.mipmap.ic_back)  
  5.           // 设置图片采样率,一般设置为RGB565,这样会比RGB888节省一半左右的内存消耗  
  6.         .config(Bitmap.Config.RGB_565)  
  7.           // 设置输出的大小,设置为我们需要的大小,避免资源浪费  
  8.         .resize(width, height)  
  9.                   // 设置是否需要缓存  
  10.         .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_CACHE)  
  11.           // 这是picasso最强大的地方我们下面看  
  12.         .transform(new Transformation())  
  13.           // 加载错误时默认的图片  
  14.         .error(R.mipmap.ic_launcher)  
  15.           // 照片的旋转角度  
  16.         .rotate(float rotate)  
  17.           // 需要展示的控件  
  18.         .into(showImg);  
  19. 上面就是Picasso基本的用法,下面需要说一下transform方法。这是毕加索提供给我们的自己处理需要的效果的接口。
    即我们自己新建一个类去实现Transformation接口,复写两个方法:
         1.transform(Bitmap source);
         本方法的参数是加载的文件的位图,方法中我们写自己的逻辑。可以裁剪图片,修改采样率,旋转等等各种操作,也就是说,平时我们可以操作bitmap改变图片的方式都可以写在这里。
         2.key() return String;
         这个方法是我们设置记录这个处理过程的方法名字,如果你写了好几个实现transform的类,那么每一个这个key返回的字符串都需要不一样,否则picasso会缓存上一次你处理的效果的bitmap。


    下面记录几种暂时我用到的处理:

    [java]  view plain  copy
      在CODE上查看代码片 派生到我的代码片
    1. /** 
    2.  * picasso加载圆形图片 
    3.  * Created by ge on 2015/8/8. 
    4.  */  
    5. public class CircleTransform implements Transformation {  
    6.     @Override  
    7.     public Bitmap transform(Bitmap source) {  
    8.         int size = Math.min(source.getWidth(), source.getHeight());  
    9.   
    10.         int x = (source.getWidth() - size) / 2;  
    11.         int y = (source.getHeight() - size) / 2;  
    12.   
    13.         Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);  
    14.         if (squaredBitmap != source) {  
    15.             source.recycle();  
    16.         }  
    17.   
    18.         Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());  
    19.   
    20.         Canvas canvas = new Canvas(bitmap);  
    21.         Paint paint = new Paint();  
    22.         BitmapShader shader = new BitmapShader(squaredBitmap,  
    23.                 BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);  
    24.         paint.setShader(shader);  
    25.         paint.setAntiAlias(true);  
    26.   
    27.         float r = size / 2f;  
    28.         canvas.drawCircle(r, r, r, paint);  
    29.   
    30.         squaredBitmap.recycle();  
    31.         return bitmap;  
    32.     }  
    33.   
    34.     @Override  
    35.     public String key() {  
    36.         return "circle";  
    37.     }  
    1.        
    2. /** 
    3.  * picasso加载正方形图片,长宽比较取最小边 
    4.  * Created by ge on 2015/7/18. 
    5.  */  
    6. public class CropSquareTransformation implements Transformation {  
    7.   
    8.     //截取从宽度和高度最小作为bitmap的宽度和高度  
    9.     @Override  
    10.     public Bitmap transform(Bitmap source) {  
    11.         int x;  
    12.         int size = Math.min(source.getWidth(), source.getHeight());  
    13.   
    14.         if (source.getWidth() >= source.getHeight()){  
    15.             x = (source.getHeight() - size) / 2;  
    16.         }else {  
    17.             x = (source.getWidth() - size) / 2;  
    18.         }  
    19.         Bitmap result = Bitmap.createBitmap(source, x, x, size, size);  
    20.         if (result != source) {  
    21.             source.recycle();  
    22.         }  
    23.   
    24.         return result;  
    25.     }  
    26.   
    27.     @Override  
    28.     public String key() {  
    29.         return "crop()";  
    30.     }  
    31. }  
    接下来的是处理照片的颜色矩阵来达到改变图片效果的例子:

       
       
    [java] view plain copy
    在CODE上查看代码片 派生到我的代码片
    1. /** 
    2.  * 照片泛黄 
    3.  * Created by ge on 15/8/20. 
    4.  */  
    5. public class YellowTransformation implements Transformation {  
    6.     @Override  
    7.     public Bitmap transform(Bitmap source) {  
    8.         Bitmap output = Bitmap.createBitmap(source.getWidth(),  
    9.                 source.getHeight(), Bitmap.Config.RGB_565);  
    10.         Canvas canvas = new Canvas(output);  
    11.         Paint paint = new Paint();  
    12.         ColorMatrix cm = new ColorMatrix();  
    13.         float[] array = { 10001000100100001000,  
    14.                 0010 };  
    15.         cm.set(array);  
    16.         paint.setColorFilter(new ColorMatrixColorFilter(cm));  
    17.         canvas.drawBitmap(source, 00, paint);  
    18.         source.recycle();  
    19.         return output;  
    20.     }  
    21.   
    22.     @Override  
    23.     public String key() {  
    24.         return "yellow()";  
    25.     }  
    26. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值