zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android最新Glide 4.0使用简介详解手机开发

Android手机开发 使用 详解 简介 最新 4.0
2023-06-13 09:20:06 时间

Glide自v3.0到v4.0使用,发生了一些比较大的改变,使用方式和编程模型有了新的变化,这些变化,使得Glide的功能更加强大,性能更加优良,提供了开放的编程接口,便于开发者自定制,以适应自有项目的定制化开发。

本例给出一些最新Android Glide 4.0的新变化、新模型的简单使用方式。
写一个简单垂直线性布局:

 ?xml version= 1.0  encoding= utf-8 ?  

 LinearLayout xmlns:android= http://schemas.android.com/apk/res/android  

 xmlns:tools= http://schemas.android.com/tools  

 android:layout_width= match_parent  

 android:layout_height= match_parent  

 android:orientation= vertical  

 tools:context= zhangphil.phildemo.MainActivity  

 

  ImageView 

 android:id= @+id/image1  

 android:layout_width= 100dp  

 android:layout_height= 100dp  /  

 

  ImageView 

 android:id= @+id/image2  

 android:layout_width= 100dp  

 android:layout_height= 100dp  /  

 

  ImageView 

 android:id= @+id/image3  

 android:layout_width= 100dp  

 android:layout_height= 100dp  /  

 

  ImageView 

 android:id= @+id/image4  

 android:layout_width= 100dp  

 android:layout_height= 100dp  /  

 

 /LinearLayout 

测试的MainActivity.java,在这里面有四个不同的新版Glide 4.0使用方式:

package zhangphil.phildemo; 

 

import android.graphics.drawable.Drawable; 

import android.support.annotation.Nullable; 

import android.support.v7.app.AppCompatActivity; 

import android.os.Bundle; 

import android.widget.ImageView; 

 

import com.bumptech.glide.Glide; 

import com.bumptech.glide.Priority; 

import com.bumptech.glide.RequestBuilder; 

import com.bumptech.glide.load.DataSource; 

import com.bumptech.glide.load.engine.DiskCacheStrategy; 

import com.bumptech.glide.load.engine.GlideException; 

import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions; 

import com.bumptech.glide.request.RequestListener; 

import com.bumptech.glide.request.RequestOptions; 

import com.bumptech.glide.request.target.Target; 

 

public class MainActivity extends AppCompatActivity { 

 private String url =  https://www.baidu.com/img/bd_logo1.png  

 

 @Override 

 protected void onCreate(Bundle savedInstanceState) { 

 super.onCreate(savedInstanceState); 

 setContentView(R.layout.activity_main); 

 

 ImageView image1 = (ImageView) findViewById(R.id.image1); 

 ImageView image2 = (ImageView) findViewById(R.id.image2); 

 ImageView image3 = (ImageView) findViewById(R.id.image3); 

 ImageView image4 = (ImageView) findViewById(R.id.image4); 

 

 load1(image1); 

 load2(image2); 

 load3(image3); 

 load4(image4); 

 } 

 

 private void load1(ImageView image) { 

 Glide.with(this) 

 .load(url) 

 .apply(new RequestOptions().circleCrop().placeholder(R.mipmap.ic_launcher)) 

 .transition(new DrawableTransitionOptions().crossFade(2000)) 

 .into(image); 

 } 

 

 private void load2(ImageView image) { 

 RequestOptions options = new RequestOptions() 

 .centerCrop() 

 .placeholder(R.mipmap.ic_launcher_round) 

 .error(android.R.drawable.stat_notify_error) 

 .priority(Priority.HIGH) 

 //.skipMemoryCache(true) 

 .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC); 

 

 Glide.with(this) 

 .load(url) 

 .apply(options) 

 .into(image); 

 } 

 

 private void load3(ImageView image) { 

 Glide.with(this) 

 .load(url) 

 .listener(new RequestListener Drawable () { 

 @Override 

 public boolean onLoadFailed(@Nullable GlideException e, Object model, Target Drawable  target, boolean isFirstResource) { 

 return false; 

 } 

 

 @Override 

 public boolean onResourceReady(Drawable resource, Object model, Target Drawable  target, DataSource dataSource, boolean isFirstResource) { 

 return false; 

 } 

 }) 

 .apply(new RequestOptions().centerInside()) 

 .thumbnail(Glide.with(this).load(R.mipmap.ic_launcher)) 

 .into(image); 

 } 

 

 private void load4(ImageView image) { 

 RequestBuilder Drawable  mRequestBuilder = Glide.with(this).load(url); 

 

 mRequestBuilder.listener(new RequestListener Drawable () { 

 @Override 

 public boolean onLoadFailed(@Nullable GlideException e, Object model, Target Drawable  target, boolean isFirstResource) { 

 return false; 

 } 

 

 @Override 

 public boolean onResourceReady(Drawable resource, Object model, Target Drawable  target, DataSource dataSource, boolean isFirstResource) { 

 return false; 

 } 

 }).transition(new DrawableTransitionOptions().crossFade(2000)) 

 .apply(new RequestOptions().circleCrop()) 

 .into(image); 

 } 

}

本例给出了几个不同的方式使用新版Glide 4.0加载网络图片,对比这四种方式,可以举一反三变化出更多使用方法。

不要忘了添加读写存储和访问网络的权限:

  uses-permission android:name= android.permission.INTERNET /  

  uses-permission android:name= android.permission.WRITE_EXTERNAL_STORAGE / 

代码运行结果:

Android最新Glide 4.0使用简介详解手机开发

附录:

1,Glide在github上的开源项目主页:https://github.com/bumptech/glide

3128.html

服务器部署程序员系统优化网站设置运维