当前位置:网站首页>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);
边栏推荐
- Dcat Admin 关闭代码生成器 登录指定地址
- Uniapp | compatibility problems in the development of (to be continued)
- Smart Tips for Frida Scripting in Kali Environment
- laravel 查询数据库获取结果如何判断是否为空?
- (3) string
- Command Execution Vulnerability
- Scrapy crawler encounters redirection 301/302 problem solution
- [Hello World教程] 使用HBuilder和Uni-app 生成一个简单的微信小程序DEMO
- v-bind usage: class dynamic binding object array style style and function method
- Orasi: 1 vulnhub walkthrough
猜你喜欢

Eric target penetration test complete tutorial

库存现金、现金管理制度、现金的账务处理、银行存款、银行存款的账务处理、银行存款的核对

面试知识点整理:Skia 架构的场景渲染

mysql 原生语句点滴学习记录

CTF之xxe

Windows下MySQL数据库报“ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:8000‘ (10061)”错误解决

2022年中高级 Android 大厂面试秘籍,为你保驾护航金九银十,直通大厂

Smart Tips for Frida Scripting in Kali Environment

(6) Design of student information management system

超级云APP,陪伴您一起成长的软件
随机推荐
Scrapy crawler encounters redirection 301/302 problem solution
一次代码审计的笔记(CVE-2018-12613 phpmyadmin文件包含漏洞)
关于我的数学建模~
【泰山众筹】模式为什么一直都这么火热?是有原因的
Debian 12 Bookworm 尝鲜记
面试必备:Android性能分析与优化实战进阶手册
cmake安装到指定目录
CTF入门笔记之SQL注入
VIKINGS: 1 vulnhub walkthrough
Shuriken: 1 vulnhub walkthrough
超级云APP,陪伴您一起成长的软件
财产清查概述、 全面清查的情况、局部清查的情况、财产清查的方法、财产清查结果的处理
(6) Design of student information management system
Phonebook
强化学习笔记:DDPG
英语每日打卡
Laravel 的关联模型 及其 预加载多个关联 with使用方法
DNS详解
c语言用栈实现计算中缀表达式
快速搭建一个网关服务,动态路由、鉴权的流程,看完秒会(含流程图)