分享

Android图片加载框架

 柠檬冰啡咖 2018-03-27

       首先说Picasso,Picasso 是 Square 公司的杰作,名字叫「毕加索,充满文艺气息,意为加载图片就像画画一样,是一门艺术。Picasso 不仅具备加载图片的强大功能,还是如此的简洁。

Picasso默认的缓存分配大小特点:
  LRU缓存占应用程序可用内存的15%
  本地缓存占到硬盘空间的2%但不超过50M并且不小于5M(前提是这种情况只在4.0以上有效果,或者你能像OKHttp那样提供
  一个本地缓存库来支持全平台)
  Picasso默认开启3个线程来进行本地与网络之间的访问
  Picasso加载图片顺序, 内存–>本地–>网络

github地址
https://github.com/square/picasso

使用实例:

  1. public class PicassoActivity extends AppCompatActivity {  
  2.   
  3.     private ImageView mLoadImg;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_loader_img);  
  9.         mLoadImg = (ImageView) findViewById(R.id.loadImg);  
  10.     }  
  11.     //点击事件  
  12.     public void loadImage(View view) {  
  13. //        basicLoad(ImageUrls.bigImages[4],mLoadImg);  
  14.   
  15. //        loadAndSaveImage(ImageUrls.bigImages[5],mLoadImg);  
  16.   
  17.         tranformationImage(ImageUrls.bigImages[6],mLoadImg);  
  18.     }  
  19.   
  20.     //1.picasso的基本用法  
  21.     private void basicLoad(String url,ImageView iv){  
  22.         Picasso.with(this)    //context  
  23.                .load(url)     //图片加载地址  
  24.                .placeholder(R.mipmap.ic_launcher)    //图片加载中显示的页面  
  25.                .error(android.R.drawable.ic_menu_delete)   //图片记载失败时显示的页面  
  26.                .noFade()       //设置淡入淡出效果  
  27.                .resize(500,400)     //自定义图片加载的大小,会根据你传入的尺寸进行采样  
  28.                .centerCrop()    //图片会被裁剪,但是质量没有影响,等比例放大  
  29. //             .centerInside()   //图片完整展示,不会被压缩或挤压,一般是等比例缩小  
  30. //             .fit()      //智能展示图片,对于图片的大小和imageview的尺寸进行了测量,计算出最佳的大小和最佳的质量显示出来  
  31.                .into(iv);    //需要加载图片的控件  
  32.     }  
  33.   
  34.     //2.Picasso 加载的图片的bitmap对象并且保存到磁盘当中  
  35.     private void loadAndSaveImage(String imgUrl,ImageView iv){  
  36.         Picasso.with(this)  
  37.                 .load(imgUrl)  
  38.                 .rotate(90f)   //简单的旋转处理,传入的数据大于0度小于360度  
  39.                 .into(target);  
  40.     }  
  41.     //Target不能写成匿名内部类形式,垃圾回收器在你获取不到bitmap的引用时,会把他回收  
  42.     private Target target = new Target() {  
  43.         @Override  
  44.         public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {  
  45.             //加载成功后,得到bitmap对象,可以对其进行操作  
  46.             mLoadImg.setImageBitmap(bitmap);  
  47.             File file = new File(getExternalCacheDir().getAbsolutePath()+File.separator+"picasso.png");  
  48.             try {  
  49.                 FileOutputStream fos = new FileOutputStream(file);  
  50.                 bitmap.compress(Bitmap.CompressFormat.PNG,100,fos);  
  51.             } catch (FileNotFoundException e) {  
  52.                 e.printStackTrace();  
  53.             }  
  54.         }  
  55.         @Override  
  56.         public void onBitmapFailed(Drawable errorDrawable) {  
  57.   
  58.         }  
  59.         @Override  
  60.         public void onPrepareLoad(Drawable placeHolderDrawable) {  
  61.   
  62.         }  
  63.     };  
  64.   
  65.     //3.Picasso可以定制图片的处理  
  66.     private void tranformationImage(String imgPaht,ImageView iv){  
  67.         Picasso.with(this)  
  68.                 .load(imgPaht)  
  69.                 .transform(new CircleTransfromation())  
  70.                 .into(iv);  
  71.     }  
  72. }  
  1. public class CircleTransfromation implements Transformation{  
  2.     @Override  
  3.     public Bitmap transform(Bitmap source) {  
  4.         //对于得到的bitmap进行操作  
  5.         //获取比较小的边作为正方形的边  
  6.         int size = Math.min(source.getWidth(),source.getHeight());  
  7.         int x = (source.getWidth()-size)/2;  
  8.         int y = (source.getHeight()-size)/2;  
  9.   
  10.         Bitmap squareBitmap = Bitmap.createBitmap(source,x,y,size,size);  
  11.         if (squareBitmap!=source){  
  12.             //说明已经经过裁剪了  
  13.             source.recycle();  
  14.         }  
  15.         //把正方形位图作为画布,画圆形  
  16.         Bitmap bm = Bitmap.createBitmap(size,size,source.getConfig());  
  17.         Canvas canvas = new Canvas(bm);  
  18.         Paint paint = new Paint();  
  19.         BitmapShader shader = new BitmapShader(squareBitmap,BitmapShader.TileMode.CLAMP,  
  20.                 BitmapShader.TileMode.CLAMP);  
  21.         paint.setShader(shader);  
  22.         paint.setAntiAlias(true);  
  23.   
  24.         float radius = size/2f;  
  25.         canvas.drawCircle(radius,radius,radius,paint);  
  26.         squareBitmap.recycle();  
  27.         return bm;  
  28.     }  
  29.   
  30.     @Override  
  31.     public String key() {  
  32.         return "circle";  
  33.     }  
  34. }  

效果:


         Google一位员工完成了Glide,Glide是基于 Picasso的,依然有保存了Picasso的简洁风格,但是在此做了大量优化与改进。Glide 默认的 Bitmap 格式是 RGB_565 格式,而Picasso默认的是 ARGB_8888 格式,这个内存开销要小一半。在磁盘缓存方面,Picasso只会缓存原始尺寸的图片,而 Glide 缓存的是多种规格,也就意味着 Glide会根据你ImageView的大小来缓存相应大小的图片尺寸,比如你ImageView大小是200*200,原图是 400*400,而使用Glide 就会缓存 200*200规格的图,而Picasso只会缓存 400*400 规格的。这个改进就会导致 Glide 比 Picasso 加载的速度要快,毕竟少了每次裁剪,重新渲染的过程。令人兴奋的是Glide 支持加载 Gif 动态图,而Picasso不支持该特性。

github地址https://github.com/bumptech/glide

使用实例:

  1. public class GlideActivity extends AppCompatActivity {  
  2.   
  3.     private ImageView mLoadImg;  
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_loader_img);  
  8.         mLoadImg = (ImageView) findViewById(R.id.loadImg);  
  9.     }  
  10.   
  11.     public void loadImage(View view) {  
  12.         basicLoad(ImageUrls.newImageUrls[0],mLoadImg);  
  13.     }  
  14.   
  15.     //1.glide的基本用法  
  16.     private void basicLoad(String imgPath,ImageView iv){  
  17.         Glide.with(this)   //使得glide更容易使用,因为能接收context,activity,fragment对象  
  18.              .load(imgPath)  
  19. //             .asGif()      //判断加载的url资源是否是gif格式的资源  
  20.              .priority(Priority.HIGH)    //设置优先级  
  21.              .placeholder(R.mipmap.ic_launcher)    //加载中显示的图片  
  22.              .error(android.R.drawable.ic_menu_delete)//加载失败显示的图片  
  23.              .centerCrop()  
  24. //             .fitCenter()    //缩放图像,整个显示在控件,尽可能的填满  
  25.             .into(iv);  
  26.     }  
  27.   
  28.     //2.获取glide加载bitmap的方法,能够显示并存储图片  
  29.     private void loadAndSaveImage(String url,ImageView iv){  
  30.   
  31.         Glide.with(this)  
  32.                 .load(url).asBitmap().into(target);  
  33.     }  
  34.   
  35.     SimpleTarget target = new SimpleTarget<Bitmap>(300,600) {  
  36.         @Override  
  37.         public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {  
  38.             mLoadImg.setImageBitmap(resource);  
  39.   
  40.             File file = new File(getExternalCacheDir().getAbsolutePath()+File.separator+"glide.png");  
  41.             try {  
  42.                 FileOutputStream fos = new FileOutputStream(file);  
  43.                 resource.compress(Bitmap.CompressFormat.PNG,100,fos);  
  44.             } catch (FileNotFoundException e) {  
  45.                 e.printStackTrace();  
  46.             }  
  47.         }  
  48.     };  
  49. }  



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多