GT库 为简单而生!接下来我们来看看 GT库 图片框架 支持的功能与具体使用方式。
目前支持功能:
1.加载图片
2.支持三级缓存,可设置是否缓存
3.占位图(图片完成前的图)
4.异常图(图片加载异常的)
5.剪切图片大小,可一键设置缩略图
7.高斯模糊
8.支持圆角图片
5.支持GIF
目前支持加载的图片资源参数类型如下:
网络地址(支持 GIF网图)、SD本地图片路径、File对象(带有图片路径的File)、byte[] 二进制流、Uri uri、Bitmap、Drawable、R资源、ImageView 图片组件
话不多说,下面我们就来看看这些功能都是怎么实现的:
准备工作 :依赖GT库
在使用GT库里封装的架构当然需要先依赖好GT库:
详细依赖教程请参看
先粘贴一下我的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="h,2:1"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="20dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
加入网络权限:
<uses-permission android:name="android.permission.INTERNET" />
第1个功能:加载简易图片(使用方法与 原本的Glide几乎没区别)
//获取图片组件 与 网络图片
String urlImg = "https://img0.baidu.com/it/u=2746042376,2078414564&f" +
"m=253&fmt=auto&app=138&f=JPEG?w=800&h=433";
ImageView imageView = findViewById(R.id.iv);
//加载一张简易的图片
GT.Glide.with(this)//构建初始化
.load(urlImg)//设置加载的网络图
.into(imageView);//设置图片控件
效果图:
第2个功能:占位图(在网络图片加载完成前,显示的过渡图片,所以占位图最好是本项目资源)
//获取图片组件 与 网络图片
String urlImg = "https://img0.baidu.com/it/u=2746042376,2078414564&f" +
"m=253&fmt=auto&app=138&f=JPEG?w=800&h=433";
ImageView imageView = findViewById(R.id.iv);
//加载一张简易的图片,且带占位图的
GT.Glide.with(this)//构建初始化
.placeholder(R.drawable.aaa)//设置占位图
.load(urlImg)//设置加载的网络图
.into(imageView);//设置图片控件
效果图:
占位图资源,提供下载:
很明显可以看到,占位图一开始就会显示出来呈现给用户看,待网络图片完全加载完毕后才会进行显示。
第3个功能:异常图(在网络图片加载错误时显示的图片)
//获取图片组件 与 网络图片
String urlImg = "https://img0.baidu.com/it/u=2746042376,2078414564&f" +
"XXXXX";//故意把图片地址写错,让网图加载错误
ImageView imageView = findViewById(R.id.iv);
//加载一张简易的图片,且带占位图的
GT.Glide.with(this)//构建初始化
.placeholder(R.drawable.aaa)//设置占位图
.error(R.drawable.eee)//设置错误图
.load(urlImg)//设置加载的网络图
.into(imageView);//设置图片控件
效果图:
因为网络图片地址被故意写错,所以导致网络图片加载异常,先是占位图显示出来,后因为网络图片加载错误,就导致 异常图 显示出来,接下来我们来看看,在断网的情况下,会怎么显示。
断网情况下 加载图片效果图:(代码同上)
异常图资源,提供下载:
在断网情况下,GT库的 GT.Glide 会直接显示 异常图 连 占位图 都不显示了,网都断了,占位图就没有显示的必要。
第4个功能:剪切图片大小,可一键设置缩略图
//获取图片组件 与 网络图片
String urlImg = "https://img0.baidu.com/it/u=2746042376,2078414564&f" +
"m=253&fmt=auto&app=138&f=JPEG?w=800&h=433";
ImageView imageView = findViewById(R.id.iv);
//加载一张简易的图片,且带占位图的
GT.Glide.with(this)//构建初始化
.loadThumbnail(urlImg,150,100)//设置剪切图片
.into(imageView);//设置图片控件
效果图:
缩略图一般会用在大图放小的情况下
第5个功能:高斯模糊
//获取图片组件 与 网络图片
String urlImg = "https://img0.baidu.com/it/u=2746042376,2078414564&f" +
"m=253&fmt=auto&app=138&f=JPEG?w=800&h=433";
ImageView imageView = findViewById(R.id.iv);
//加载一张简易的图片,切设置高斯模糊
GT.Glide.with(this)//构建初始化
.blurTransformation(20)//设置 高斯模糊度
.load(urlImg)//设置加载的网络图
.into(imageView);//设置图片控件
效果图:
这近视眼 身临其境般的感觉是咋回事~~~
第6个功能:支持圆角图片
//获取图片组件 与 网络图片
String urlImg = "https://img0.baidu.com/it/u=2746042376,2078414564&f" +
"m=253&fmt=auto&app=138&f=JPEG?w=800&h=433";
ImageView imageView = findViewById(R.id.iv);
//加载一张简易的图片,切设置高斯模糊
GT.Glide.with(this)//构建初始化
.roundedCorners(50)//设置 四个角的 圆角度数
.load(urlImg)//设置加载的网络图
.into(imageView);//设置图片控件
效果图:
扩展内容:设置不同角度数
//获取图片组件 与 网络图片
String urlImg = "https://img0.baidu.com/it/u=2746042376,2078414564&f" +
"m=253&fmt=auto&app=138&f=JPEG?w=800&h=433";
ImageView imageView = findViewById(R.id.iv);
//加载一张简易的图片,切设置高斯模糊
GT.Glide.with(this)//构建初始化
.roundedCorners(200,30,30,200)//设置四个角分别的原角度数
.load(urlImg)//设置加载的网络图
.into(imageView);//设置图片控件
效果图:
PS: 是不是非常简单易用,GT库不止这一个好框架。
第7个功能:支持GIF动态图
//获取图片组件 与 网络动态图片
String gifImg = "https://s1.chu0.com/src/img/gif/60/" +
"606e2efad8ea4417a4e101fa1285d609.gif" +
"?e=1735488000&token=1srnZGLKZ0Aqlz6dk7yF4SkiYf4eP-" +
"YrEOdM1sob:IA5gbzlKc-NNfpArFhy-5xGKjUg=";
ImageView imageView = findViewById(R.id.iv);
//加载一张简易的图片,切设置高斯模糊
GT.Glide.with(this)//构建初始化
.asGif()//设置本次加载为 GIF 动态图
.load(gifImg)//设置加载的网络图
.into(imageView);//设置图片控件
效果图:
注意:目前 GT.Glide 仅仅支持加载网络动态图,对本地加载动态图片还在优化中,但GT库也提供了一套专门加载本地动态图的组件 GTImageView,使用的方法也非常简单,该组件可以当说是 ImageView 的加强版,支持的圆角,动态图等,用他来实现圆角头像再适合不过了,使用起来非常方便。
先看效果图:
第二步:xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- 加载 静态图 -->
<view
android:id="@+id/gtIV"
class="com.gsls.gt.GT$ViewUtils$GTImageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:src="@mipmap/gt_logo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- 加载 圆角静态图 -->
<view
android:id="@+id/gtIV2"
class="com.gsls.gt.GT$ViewUtils$GTImageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:src="@mipmap/gt_logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:radius_top_left="30dp"
app:radius_top_right="30dp" />
<!-- 加载 本地动态图 -->
<view
android:id="@+id/gtIV3"
class="com.gsls.gt.GT$ViewUtils$GTImageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/gt_logo2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- 加载 网络圆角动态图 -->
<view
android:id="@+id/gtIV4"
class="com.gsls.gt.GT$ViewUtils$GTImageView"
android:layout_width="150dp"
android:layout_height="150dp"
app:radius_top_left="50dp"
app:radius_top_right="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<!-- 加载 网络动态图 -->
<view
android:id="@+id/gtIV5"
class="com.gsls.gt.GT$ViewUtils$GTImageView"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
第三步:动态加载 Activity代码:
public class MainActivity extends AppCompatActivity {
String gif = "https://s1.chu0.com/src/img/gif/52/52784937bc374c779332150318a75baf.gif?imageMogr2/auto-orient/thumbnail/!231x201r/gravity/Center/crop/231x201/quality/85/&e=1735488000&token=1srnZGLKZ0Aqlz6dk7yF4SkiYf4eP-YrEOdM1sob:SXbWiapi5fG0MM5V-0hwE43cvnY=";
String gif2 = "https://s1.chu0.com/src/img/gif/60/606e2efad8ea4417a4e101fa1285d609.gif?e=1735488000&token=1srnZGLKZ0Aqlz6dk7yF4SkiYf4eP-YrEOdM1sob:IA5gbzlKc-NNfpArFhy-5xGKjUg=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GT.ViewUtils.GTImageView gtImageView4 = findViewById(R.id.gtIV4);
gtImageView4.setGifResource(gif,this);
GT.ViewUtils.GTImageView gtImageView5 = findViewById(R.id.gtIV5);
gtImageView5.setGifResource(gif2,this);
}
}
PS:如果想实现 圆角头像,博主建议使用 GTImageView 这个控件去实现。
满满干货,目前该组件还有一些没完善的,今后有时间就会去完善。
点个关注点个赞呗(〃'▽'〃) 关注博主最新发布库:GitHub - 1079374315/GT