• Android中的Glide加载图片


    注意:在Android Studio的项目的build.gradle中添加:

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

    然后同步一下

    目录:

    1.     使用Glide结合列表的样式进行图片加载
    2.     如果使用的是RecyclerView,可以在Adapter的onBindViewHolder方法中使用
    3.     当加载网络图片时,由于加载过程中图片未能及时显示,此时可能需要设置等待时的图片,通过placeHolder()方法
    4.     当加载图片失败时,通过error(Drawable drawable)方法设置加载失败后的图片显示
    5.     图片的缩放,centerCrop()和fitCenter()
    6.     显示gif动画
    7.     显示本地视频
    8.     缓存策略
    9.     优先级,设置图片加载的顺序
    10.     当不需要将加载的资源直接放入到ImageView中而是想获取资源的Bitmap对象
    11.     集成网络栈(okHttp,Volley)
     
    1.使用Glide结合列表的样式进行图片加载:
            1)    如果使用的是ListView,可以直接在Adapter的getView方法中使用:                
    1. @Override
    2. public View getView(int position, View convertView, ViewGroup parent) {
    3. if (null == convertView) {
    4. //.....
    5. }
    6. Glide
    7. .with(context)
    8. .load(imageUrls[position])
    9. .into(holder.imageView);
    10. return convertView;
    11. }
     
            2)    如果使用的是RecyclerView,可以在Adapter的onBindViewHolder方法中使用:        
    1. @Override
    2. public void onBindViewHolder(RVViewHolder holder, int position) {
    3. Glide.with(MainActivity.this)
    4. .load(args[position])
    5. .into(holder.imageView);
    6. }
           
             3)    当加载网络图片时,由于加载过程中图片未能及时显示,此时可能需要设置等待时的图片,通过placeHolder()方法: 
    1. Glide
    2. .with(context)
    3. .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    4. .placeholder(R.mipmap.ic_launcher) // can also be a drawable
    5. .into(imageViewPlaceholder);
            
            4)    当加载图片失败时,通过error(Drawable drawable)方法设置加载失败后的图片显示:
    1. Glide
    2. .with(context)
    3. .load("http://futurestud.io/non_existing_image.png")
    4. .error(R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded
    5. .into(imageViewError);
     
            5)    图片的缩放,centerCrop()和fitCenter():        
    1. //使用centerCrop是利用图片图填充ImageView设置的大小,如果ImageView的
    2. //Height是match_parent则图片就会被拉伸填充
    3. Glide.with(MainActivity.this)
    4. .load(args[position])
    5. .centerCrop()
    6. .into(holder.imageView);
    1. //使用fitCenter即缩放图像让图像都测量出来等于或小于 ImageView 的边界范围
    2. //该图像将会完全显示,但可能不会填满整个 ImageView。
    3. Glide.with(MainActivity.this)
    4. .load(args[position])
    5. .fitCenter()
    6. .into(holder.imageView);
            6)    显示gif动画:
    1. Glide
    2. .with( context )
    3. .load( gifUrl )
    4. .asGif() //判断加载的url资源是否为gif格式的资源
    5. .error( R.drawable.full_cake )
    6. .into( imageViewGif );
     
            7)    显示本地视频
    1. String filePath = "/storage/emulated/0/Pictures/example_video.mp4";
    2. Glide
    3. .with( context )
    4. .load( Uri.fromFile( new File( filePath ) ) )
    5. .into( imageViewGifAsBitmap );
            8)    缓存策略:
    1. Glide
    2. .with( context )
    3. .load( Images[0] )
    4. .skipMemoryCache( true ) //跳过内存缓存
    5. .into( imageViewInternet );
    1. Glide
    2. .with( context )
    3. .load( images[0] )
    4. .diskCacheStrategy( DiskCacheStrategy.NONE ) //跳过硬盘缓存
    5. .into( imageViewInternet );
    • DiskCacheStrategy.NONE 什么都不缓存
    • DiskCacheStrategy.SOURCE 仅仅只缓存原来的全分辨率的图像
    • DiskCacheStrategy.RESULT 仅仅缓存最终的图像,即降低分辨率后的(或者是转换后的)
    • DiskCacheStrategy.ALL 缓存所有版本的图像(默认行为
            9)    优先级,设置图片加载的顺序:
        
    • Priority.LOW
    • Priority.NORMAL
    • Priority.HIGH
    • Priority.IMMEDIATE   
    1. private void loadImageWithHighPriority() {
    2. Glide
    3. .with( context )
    4. .load( mages[0] )
    5. .priority( Priority.HIGH )
    6. .into( imageViewHero );
    7. }
    8. private void loadImagesWithLowPriority() {
    9. Glide
    10. .with( context )
    11. .load( images[1] )
    12. .priority( Priority.LOW )
    13. .into( imageViewLowPrioLeft );
    14. Glide
    15. .with( context )
    16. .load( images[2] )
    17. .priority( Priority.LOW )
    18. .into( imageViewLowPrioRight );
    19. }
     
            10)    当不需要将加载的资源直接放入到ImageView中而是想获取资源的Bitmap对象:
    1. //括号中的300,600代表宽和高但是未有作用
    2. SimpleTarget target = new SimpleTarget<Bitmap>(300,600) {
    3. @Override
    4. public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
    5. holder.imageView.setImageBitmap(resource);
    6. }
    7. };
    8. Glide.with(MainActivity.this)
    9. .load(args[position])
    10. .asBitmap()
    11. .into(target);
            
            11)    集成网络栈(okHttp,Volley):  
    1. dependencies {
    2. // your other dependencies
    3. // ...
    4. // Glide
    5. compile 'com.github.bumptech.glide:glide:3.6.1'
    6. // Glide's OkHttp Integration
    7. compile 'com.github.bumptech.glide:okhttp-integration:1.3.1@aar'
    8. compile 'com.squareup.okhttp:okhttp:2.5.0'
    9. }
    1. dependencies {
    2. // your other dependencies
    3. // ...
    4. // Glide
    5. compile 'com.github.bumptech.glide:glide:3.6.1'
    6. // Glide's Volley Integration
    7. compile 'com.github.bumptech.glide:volley-integration:1.3.1@aar'
    8. compile 'com.mcxiaoke.volley:library:1.0.8'
    9. }
  • 相关阅读:
    Python float() 函数
    Python bytearray() 函数
    scanner.nextInt()与scanner.nextDouble
    Scanner对象next与nextLine
    JavaDoc
    包机制
    运算符要点
    变量与常量
    test
    类型转换
  • 原文地址:https://www.cnblogs.com/changyiqiang/p/6135477.html
Copyright © 2020-2023  润新知