Ali P7 Mobile Internet Architect Advanced Video (Updated Daily) Free Learning Click: https://space.bilibili.com/474380680
This article will use Glide to illustrate the Picture Loading Frame selection:
First, add dependencies
implementation 'com.github.bumptech.glide:glide:4.5.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'
Then add access to the network
<uses-permission android:name="android.permission.INTERNET" />
1. Common methods
1. Load pictures to imageView
Glide.with(Context context).load(Strint url).into(ImageView imageView);
2. Loading various forms of pictures into ImageView
// Load local pictures File file = new File(getExternalCacheDir() + "/image.jpg"); Glide.with(this).load(file).into(imageView); // Load application resources int resource = R.drawable.image; Glide.with(this).load(resource).into(imageView); // Load Binary Stream byte[] image = getImageBytes(); Glide.with(this).load(image).into(imageView); // Loading Uri Objects Uri imageUri = getImageUri(); Glide.with(this).load(imageUri).into(imageView);
3. Load with placeholder
Glide.with(this).load(url).placeholder(R.drawable.loading).into(imageView);
The purpose of a placeholder map is to display a picture to the user in advance before the destination picture is loaded.
4. Place placeholder on load failure
Glide.with(this).load(url).placeholder(R.drawable.loading).error(R.drawable.error) .diskCacheStrategy(DiskCacheStrategy.NONE)//Turn off Glide's hard disk caching mechanism .into(imageView); //DiskCacheStrategy.NONE: Indicates that nothing is cached. //DiskCacheStrategy.SOURCE: Indicates that only the original pictures are cached. //DiskCacheStrategy.RESULT: Indicates that only converted pictures are cached (default option). //DiskCacheStrategy.ALL: Indicates that both the original and converted pictures are cached.
5. Load pictures in the specified format--designate as still pictures
Glide.with(this) .load(url) .asBitmap()//Only static pictures are loaded, and only the first frame is loaded if it is a git picture. .placeholder(R.drawable.loading) .error(R.drawable.error) .diskCacheStrategy(DiskCacheStrategy.NONE) .into(imageView);
6. Loading dynamic pictures
Glide.with(this) .load(url) .asGif()//Load dynamic pictures, and if the existing pictures are non-gif pictures, load the error placeholder directly. .placeholder(R.drawable.loading) .error(R.drawable.error) .diskCacheStrategy(DiskCacheStrategy.NONE) .into(imageView);
7. Load pictures of a specified size
Glide.with(this) .load(url) .placeholder(R.drawable.loading) .error(R.drawable.error) .diskCacheStrategy(DiskCacheStrategy.NONE) .override(100, 100)//Specify picture size .into(imageView);
8. Turn off the frame's memory caching mechanism
Glide.with(this) .load(url) .skipMemoryCache(true) //When the incoming parameter is false, the memory cache is turned off. .into(imageView);
9. Turn off hard disk caching
Glide.with(this) .load(url) .diskCacheStrategy(DiskCacheStrategy.NONE) //Turn off hard disk caching .into(imageView); //Other parameters represent: //DiskCacheStrategy.NONE: Indicates that nothing is cached. //DiskCacheStrategy.SOURCE: Indicates that only the original pictures are cached. //DiskCacheStrategy.RESULT: Indicates that only converted pictures are cached (default option). //DiskCacheStrategy.ALL: Indicates that both the original and converted pictures are cached.
10. Solution when token exists for referenced url-->Rewrite GlideUrl method for 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 load the picture:
Glide.with(this) .load(new MyGlideUrl(url)) .into(imageView);
11. Use Glide to load pictures into different controls or into different ways of using them
(1) Get an example of a picture
//1. Picture instances can be obtained by constructing a target yourself SimpleTarget<GlideDrawable> simpleTarget = new SimpleTarget<GlideDrawable>() { @Override public void onResourceReady(GlideDrawable resource, GlideAnimation glideAnimation) { imageView.setImageDrawable(resource); } }; //2. Record an instance of a picture on a specified image view, or do something else 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) Load pictures anywhere
/* *Load Picture as Control Background */ 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); } } //Reference picture to specified control as background 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 implements preloading
//a. Preloaded code Glide.with(this) .load(url) .diskCacheStrategy(DiskCacheStrategy.SOURCE) .preload(); //preload() has two overloads // 1. Overload with parameters to set the pre-loaded picture size; //2. The picture loaded without parameters is the original size; //b. Use pre-loaded pictures Glide.with(this) .load(url) .diskCacheStrategy(DiskCacheStrategy.SOURCE) .into(imageView);
Keep in mind that the parameter within the diskCacheStrategy() method must be set to:'DiskCacheStrategy.SOURCE', otherwise the preload may fail, requiring a reload when the picture is displayed.
13. Glide for picture download
Use the downloadOnly(int width, int height) or downloadOnly(Y target) method instead of the in (view) method.
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) The downloadOnly(int width, int height) method with two parameters indicates the specified download size for downloading within a child thread;
(2) A parameter's downloadOnly(Y target) method is downloaded in the main thread
(3) The target.get() method can get the path to save the download file;
Use downloaded pictures
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: The parameter of the diskCacheStrategy() method should be DiskCacheStrategy.SOURCE or DiskCacheStrategy.ALL or it may cause the picture to be reloaded when it is loaded into the control.
13. Listen for Glide loading status
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) The onException() method indicates that the load failed, and onResourceReady() indicates that the load succeeded;
(2) Each method has a boolean return value, false means unprocessed, true means processed.
14. Graphic transformation function of Glide
(1) Disable graphic transformation
Glide.with(this) .load(url) .dontTransform() .into(imageView);
This method is global, making it impossible to transform pictures from other places.
Modify Method
Glide.with(this) .load(url) .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) .into(imageView);
Setting size by override() method
(2) Simple graphic transformation
Glide.with(this) .load(url) .centerCrop() .into(imageView); Glide.with(this) .load(url) .fitCenter() .into(imageView);
The centerCrop() method fills the full screen with the original aspect ratio and fitCenter() method clips the central area of the original image to set up the picture.
(3) The override() method is used in conjunction with the centerCrop() method
String url = "http://cn.bing.com/az/hprichbg/rb/AvalancheCreek_ROW11173354624_1920x1080.jpg"; Glide.with(this) .load(url) .override(500, 500) .centerCrop() .into(imageView);
(4) Complex image transformation
First you need to introduce another Third-party framework .
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 Visualization
Glide.with(this) .load(url) .bitmapTransform(new BlurTransformation(this)) .into(imageView);
Black and White Picture
Glide.with(this) .load(url) .bitmapTransform(new GrayscaleTransformation(this)) .into(imageView);
Multiple attributes used simultaneously
Glide.with(this) .load(url) .bitmapTransform(new BlurTransformation(this), new GrayscaleTransformation(this)) .into(imageView);
There are more interesting attributes to view on the framework website: https://github.com/wasabeef/glide-transformations
Original Link: https://www.jianshu.com/p/791ee473a89b
Ali P7 Mobile Internet Architect Advanced Video (Updated Daily) Free Learning Click: https://space.bilibili.com/474380680