1、在build.gradle文件中添加依赖
implementation 'com.github.bumptech.glide:glide:4.13.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'
2、在manifes.xml中添加权限
<uses-permission android:name="android.permission.INTERNET"/>
3、在代码中显示
下载图片的地址:
val url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp01%2F1ZZQ214233446-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1654236822&t=168186152bef10b8ac90a070ac4e5142";
下载图片完成后所要显示在的view
val imageOne =findViewById<ImageView>(R.id.iv_one)
通过传入context初始化glide, load后传入下载地址,into方法传入所要显示在哪个view上
Glide.with(this)
.load(url)
.into(imageOne)
加载完成界面显示
4、显示圆角图片
.circleCrop()
代码:
val url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp01%2F1ZZQ214233446-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1654236822&t=168186152bef10b8ac90a070ac4e5142";
val imageTwo =findViewById<ImageView>(R.id.iv_two)
Glide.with(this)
.load(url)
.fitCenter()
.circleCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.ic_launcher_background)
.into(imageTwo)
显示结果:
5、重置图片大小
override(100,400)
代码:
val imageThree =findViewById<ImageView>(R.id.iv_three)
Glide.with(this)
.load(url)
.override(100,400)
.error(R.drawable.ic_launcher_background)
.into(imageThree)
运行结果: