Photos of the load frame loading frame selection (a) novella

 

Ali P7 mobile Internet architect, Advanced Video (Daily update) free Learning please click: https://space.bilibili.com/474380680
This article will elaborate pictures to load the selection frame by using Glide:

First, add dependencies

implementation 'com.github.bumptech.glide:glide:4.5.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'

After the addition of network access permissions

<uses-permission android:name="android.permission.INTERNET" />

A common approach

1, Load picture to imageView

Glide.with(Context context).load(Strint url).into(ImageView imageView);

2, various forms of image loading to ImageView

// 加载本地图片
File file = new File(getExternalCacheDir() + "/image.jpg");
Glide.with(this).load(file).into(imageView);

// 加载应用资源
int resource = R.drawable.image;
Glide.with(this).load(resource).into(imageView);

// 加载二进制流
byte[] image = getImageBytes();
Glide.with(this).load(image).into(imageView);

// 加载Uri对象
Uri imageUri = getImageUri();
Glide.with(this).load(imageUri).into(imageView);

3, FIG loaded with placeholder

Glide.with(this).load(url).placeholder(R.drawable.loading).into(imageView);

Figure placeholder for the purpose at the time of the purpose of the picture has not been loaded out, in advance of a picture displayed to the user;
4, failed to load placed placeholder

Glide.with(this).load(url).placeholder(R.drawable.loading).error(R.drawable.error)
     .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
     .into(imageView);

//DiskCacheStrategy.NONE: 表示不缓存任何内容。
//DiskCacheStrategy.SOURCE: 表示只缓存原始图片。
//DiskCacheStrategy.RESULT: 表示只缓存转换过后的图片(默认选项)。
//DiskCacheStrategy.ALL : 表示既缓存原始图片,也缓存转换过后的图片。

5, load the specified format images - designated as a still picture

Glide.with(this)
     .load(url)
     .asBitmap()//只加载静态图片,如果是git图片则只加载第一帧。
     .placeholder(R.drawable.loading)
     .error(R.drawable.error)
     .diskCacheStrategy(DiskCacheStrategy.NONE)
     .into(imageView);

6, load dynamic picture

Glide.with(this)
     .load(url)
     .asGif()//加载动态图片,若现有图片为非gif图片,则直接加载错误占位图。
     .placeholder(R.drawable.loading)
     .error(R.drawable.error)
     .diskCacheStrategy(DiskCacheStrategy.NONE)
     .into(imageView);

7, load the specified size image

Glide.with(this)
     .load(url)
     .placeholder(R.drawable.loading)
     .error(R.drawable.error)
     .diskCacheStrategy(DiskCacheStrategy.NONE)
     .override(100, 100)//指定图片大小
     .into(imageView);

8, close the frame memory caching mechanism

Glide.with(this)
     .load(url)
     .skipMemoryCache(true)  //传入参数为false时,则关闭内存缓存。
     .into(imageView);

9, turn off the hard disk cache

Glide.with(this)
     .load(url)
     .diskCacheStrategy(DiskCacheStrategy.NONE)     //关闭硬盘缓存操作
     .into(imageView);

//其他参数表示:
//DiskCacheStrategy.NONE: 表示不缓存任何内容。
//DiskCacheStrategy.SOURCE: 表示只缓存原始图片。
//DiskCacheStrategy.RESULT: 表示只缓存转换过后的图片(默认选项)。
//DiskCacheStrategy.ALL : 表示既缓存原始图片,也缓存转换过后的图片。

10, when the present token solutions cited url -> GlideUrl rewriting method Glide

public class MyGlideUrl extends GlideUrl {

    private String mUrl;

    public MyGlideUrl(String url) {
        super(url);
        mUrl = url;
    }

    @Override
    public String getCacheKey() {
        return mUrl.replace(findTokenParam(), "");
    }

    private String findTokenParam() {
        String tokenParam = "";
        int tokenKeyIndex = mUrl.indexOf("?token=") >= 0 ? mUrl.indexOf("?token=") : mUrl.indexOf("&token=");
        if (tokenKeyIndex != -1) {
            int nextAndIndex = mUrl.indexOf("&", tokenKeyIndex + 1);
            if (nextAndIndex != -1) {
                tokenParam = mUrl.substring(tokenKeyIndex + 1, nextAndIndex + 1);
            } else {
                tokenParam = mUrl.substring(tokenKeyIndex);
            }
        }
        return tokenParam;
    }

}

Then loadable Photo:

Glide.with(this)
     .load(new MyGlideUrl(url))
     .into(imageView);

11, the image is loaded into Glide using different controls or use a different load
(1), examples of pictures to get

//1、通过自己构造 target 可以获取到图片实例
SimpleTarget<GlideDrawable> simpleTarget = new SimpleTarget<GlideDrawable>() {
    @Override
    public void onResourceReady(GlideDrawable resource, GlideAnimation glideAnimation) {
        imageView.setImageDrawable(resource);
    }
};

//2、将图片实例记载到指定的imageview上,也可以做其他的事情
public void loadImage(View view) {
    String url = "http://cn.bing.com/az/hprichbg/rb/TOAD_ZH-CN7336795473_1920x1080.jpg";
    Glide.with(this)
         .load(url)
         .into(simpleTarget);
}

(2), the image is loaded into any position

/*
*将图片加载为控件背景
*/
public class MyLayout extends LinearLayout {

    private ViewTarget<MyLayout, GlideDrawable> viewTarget;

    public MyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        viewTarget = new ViewTarget<MyLayout, GlideDrawable>(this) {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation glideAnimation) {
                MyLayout myLayout = getView();
                myLayout.setImageAsBackground(resource);
            }
        };
    }

    public ViewTarget<MyLayout, GlideDrawable> getTarget() {
        return viewTarget;
    }

    public void setImageAsBackground(GlideDrawable resource) {
        setBackground(resource);
    }

}

//引用图片到指定控件作为背景
public class MainActivity extends AppCompatActivity {

    MyLayout myLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myLayout = (MyLayout) findViewById(R.id.background);
    }

    public void loadImage(View view) {
        String url = "http://cn.bing.com/az/hprichbg/rb/TOAD_ZH-CN7336795473_1920x1080.jpg";
        Glide.with(this)
             .load(url)
             .into(myLayout.getTarget());
    }

}

12, Glide achieve preload

//a、预加载代码
Glide.with(this)
     .load(url)
     .diskCacheStrategy(DiskCacheStrategy.SOURCE)
     .preload();

//preload() 有两种重载
 // 1、带有参数的重载,参数作用是设置预加载的图片大小;
//2、不带参数的表示加载的图片为原始尺寸;

//b、使用预加载的图片
Glide.with(this)
     .load(url)
     .diskCacheStrategy(DiskCacheStrategy.SOURCE)
     .into(imageView);

Remember: In diskCacheStrategy () method must set the parameters as: "DiskCacheStrategy.SOURCE", or it may preload fail, resulting in the display when the picture needs to be reloaded.

13, Glide achieve download images
using downloadOnly(int width, int height)or downloadOnly(Y target)methods alternative into(view)methods.

public void downloadImage(View view) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                String url = "http://cn.bing.com/az/hprichbg/rb/TOAD_ZH-CN7336795473_1920x1080.jpg";
                final Context context = getApplicationContext();
                FutureTarget<File> target = Glide.with(context)
                                                 .load(url)
                                                 .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
                final File imageFile = target.get();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, imageFile.getPath(), Toast.LENGTH_LONG).show();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}

(1), there are two parameters of downloadOnly(int width, int height)the method indicates that the specified download size for downloading in the sub-thread;
(2), a parameter of downloadOnly(Y target)a method for download in the main thread
(3), target.get()the method can obtain the download file path ;

Use of downloaded pictures mode

public void loadImage(View view) {
    String url = "http://cn.bing.com/az/hprichbg/rb/TOAD_ZH-CN7336795473_1920x1080.jpg";
    Glide.with(this)
            .load(url)
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(imageView);
}

Note: Parameter diskCacheStrategy () method be DiskCacheStrategy.SOURCEor DiskCacheStrategy.ALLit may cause the picture to load the control, the need to reload.

13, listening state Glide loaded

public void loadImage(View view) {
    String url = "http://cn.bing.com/az/hprichbg/rb/TOAD_ZH-CN7336795473_1920x1080.jpg";
    Glide.with(this)
            .load(url)
            .listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target,
                    boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model,
                    Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    return false;
                }
            })
            .into(imageView);
}

(1), onException()the method represents a load failure, onResourceReady()completes the loading;
(2), each method has a return value boolean, represents untreated to false, true represent processing.

14, Glide graphics transformation function
(1), disable the graphical conversion function

Glide.with(this)
     .load(url)
     .dontTransform()
     .into(imageView);

When this method is a global, leading the rest of the picture can not be graphically transformed.

Modification method

Glide.with(this)
     .load(url)
     .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
     .into(imageView);

By setting the size of the override () method

(2), simple graphics transformation

Glide.with(this)
     .load(url)
     .centerCrop()
     .into(imageView);

Glide.with(this)
     .load(url)
     .fitCenter()
     .into(imageView);

By centerCrop()the method according to the original and full of full-screen aspect ratio fitCenter()method for the central area of the original image cropping the picture settings.

(3), override()the method and centerCrop()the method with the use of

String url = "http://cn.bing.com/az/hprichbg/rb/AvalancheCreek_ROW11173354624_1920x1080.jpg";
Glide.with(this)
     .load(url)
     .override(500, 500)
     .centerCrop()
     .into(imageView);

 
6433394-f6a2669093a71fac.png
image

(4), complex image transformation
firstly need to introduce a third party frameworks .

dependencies {
    implementation 'jp.wasabeef:glide-transformations:3.3.0'
    // If you want to use the GPU Filters
    implementation 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1'
}

Second, some examples:
Picture blur

Glide.with(this)
     .load(url)
     .bitmapTransform(new BlurTransformation(this))
     .into(imageView);

Black and white picture of

Glide.with(this)
     .load(url)
     .bitmapTransform(new GrayscaleTransformation(this))
     .into(imageView);

Multiple properties simultaneously

Glide.with(this)
     .load(url)
     .bitmapTransform(new BlurTransformation(this), new GrayscaleTransformation(this))
     .into(imageView);

There are more fun properties, please see the official website of the frame: https://github.com/wasabeef/glide-transformations

Original link: https://www.jianshu.com/p/791ee473a89b
Ali P7 mobile Internet architect, Advanced Video (updated daily) is free to learn, please click: https://space.bilibili.com/474380680

Guess you like

Origin www.cnblogs.com/Android-Alvin/p/11990580.html