当前位置:网站首页>Glide中图片处理
Glide中图片处理
2022-08-02 03:27:00 【浮空over】
文章目录
一、加载本地资源
1.在清单文件中增加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.加载
其实Glide加载本地图片和加载网络图片调用的方法是一样的,唯一的区别是说加载SD卡的图片需要SD卡的权限,加载网络需要网络权限
//本地文件
File file = new File(Environment.getExternalStorageDirectory(), "xiayu.png");
//加载图片
Glide.with(this).load(file).into(iv);
一、Glide-图片预处理(圆角,高斯模糊等)
1.创建一个类继承BitmapTransformation
需要实现两个方法,其中transform方法里面能拿到bitmap对象,这里就是对图片做处理的地方
1.创建一个类继承BitmapTransformation
需要实现两个方法,其中transform方法里面能拿到bitmap对象,这里就是对图片做处理的地方
public class CornersTransform extends BitmapTransformation {
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
//TODO
}
@Override
public String getId() {
//TODO
}
}
2.使用
通过调用transform方法就能展示处理后的图片
Glide.with(this).load(url).transform(new CornersTransform()).into(iv1);
3.圆角处理
- 自定义Transformation
public class CornersTransform extends BitmapTransformation {
private float radius;
public CornersTransform(Context context) {
super(context);
radius = 10;
}
public CornersTransform(Context context, float radius) {
super(context);
this.radius = radius;
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return cornersCrop(pool, toTransform);
}
private Bitmap cornersCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
return result;
}
@Override
public String getId() {
return getClass().getName();
}
}
- 使用:
Glide.with(this).load(url).transform(new CornersTransform(this,50)).into(iv1);
- 使用多个transform
transform方法是不支持多次调用的,如果你调用了两次,那么第二次的会覆盖了第一次的效果
但是他有一个重载的方法可以传入多个对象,这样传入的变形器都能够生效
Glide.with(this).load(url).transform(new
CircleTransform(this),new CornersTransform(this,50)).into(iv1);
二、Glide-加载Gif和MP4
1.简单加载
Glide.with(this).load(mGifUrl).placeholder(R.mipmap.place).error(R.mipmap.icon_photo_error).into(mIv);
2.把gif当作普通图片加载(asBitmap)
Glide.with(this).load(mGifUrl).asBitmap().placeholder(R.mipmap.place).error(R.mipmap.icon_photo_error).into(mIv);
3.检查是否gif(asGif)
如果你希望加载的只是gif,如果不是gif就显示错误图片,那么只用加上asGif方法
Glide.with(this).load(mGifUrl).asGif().placeholder(R.mipmap.place).error(R.mipmap.icon_photo_error).into(mIv);
4.加载本地视频缩略图
直接放file文件进去
Glide只会加载本地视频的第一帧,也就是缩略图
mVideoFile = new File(Environment.getExternalStorageDirectory(), "xiayu.mp4");
Glide.with(this).load(mVideoFile).placeholder(R.mipmap.place).error(R.mipmap.icon_photo_error).into(mIv);
边栏推荐
- SQL注入(7)
- 还原最真实、最全面的一线大厂面试题
- 链动2+1无限循环系统,2022年起盘成功率超高的模式
- 元宇宙:为何互联网大佬纷纷涉足?元宇宙跟NFT是什么关系?
- 重点考:从债劵的角度来看交易性金融资产
- 管理会计(对内)指引、管理会计要素及其具体内容(可能考,考前记一下,推荐记一下四个大点即可)、
- 如何一步一步的:玩转全民拼购!
- 政府会计的概念、政府会计标准体系、政府会计的特点(会形成小考点)、政府会计要素及其确认和计量、政府预算会计要素、政府财务会计要素
- 【一句话攻略】彻底理解JS中的回调(Callback)函数
- (6) Design of student information management system
猜你喜欢
laravel 写api接口时 session获取不到处理办法
关于我的大创、论文~
A network security guinea pig's learning path - scripting of advanced usage of nmap
The roll call system and array elements find maximum and minimum values for sorting of objects
Pycharm packages the project as an exe file
会计凭证概述、原始凭证、原始凭证的种类、原始凭证的基本内容、原始凭证的填制要求、原始凭证的审核
Larave 自定义公共函数以及引入使用
2022年中高级 Android 大厂面试秘籍,为你保驾护航金九银十,直通大厂
Solve the problem that the 5+APP real machine test cannot access the background (same local area network)
命令执行漏洞
随机推荐
SATA M2 SSD 无法安装系统的解决方法
uniapp | Problems with the use of the official map component
快速搭建一个网关服务,动态路由、鉴权的流程,看完秒会(含流程图)
利用cookie获取admin权限 CTF基础题
CTF entry md5
A code audit notes (CVE - 2018-12613 phpmyadmin file contains loopholes)
链动2+1无限循环系统,2022年起盘成功率超高的模式
Laravel 登录,中间件和路由分组
最简单的FRP内网穿透教程
laravel 查询数据库获取结果如何判断是否为空?
Windows下MySQL数据库报“ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:8000‘ (10061)”错误解决
Laravel 的关联模型 及其 预加载多个关联 with使用方法
uniapp发布到微信小程序:分包、删减代码全过程
The first time to tear the code by hand, how to solve the problem of full arrangement
面试知识点整理:Skia 架构的场景渲染
解决flex布局warp自动换行下最后一行居中问题
Sensitive information leakage
SQL注入(6)
Orasi: 1 vulnhub walkthrough
聊聊MySQL的10大经典错误