当前位置:网站首页>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);
边栏推荐
- OPENSSL基本实验以及OPENSSL详解
- 记账凭证的种类、记账凭证的基本内容、记账凭证的填制要求、记账凭证的审核
- Laravel随笔记录
- file contains vulnerabilities
- hackmyvm-hopper预排
- centos8 安装搭建php环境
- WeChat applet development video loading: [Rendering layer network layer error] Failed to load media
- 一个网络安全小白鼠的学习之路——nmap的基本使用
- 广告电商「私域打工人」职业前景:你离月薪6万,还差多远?
- How to determine the direction based on two coordinate points on the map
猜你喜欢

The CTF introductory notes of SQL injection

VIKINGS: 1 vulnhub walkthrough

考(重点理解哪些属于其他货币资金)、其他货币资金的内容、其他货币资金的账务处理(银行汇票存款、银行本票存款、信用卡存款、信用证保证金存款、存出投资款、外埠存款)

hackmyvm: kitty walkthrough

OpenCore 黑苹果安装教程

解密:链动2+1的商业模式

【一句话攻略】彻底理解JS中的回调(Callback)函数

解决flex布局warp自动换行下最后一行居中问题

A network security guinea pig's learning path - scripting of advanced usage of nmap

Pycharm packages the project as an exe file
随机推荐
在 UUP dump 被墙的情况下如何用 UUP 下载 ISO 镜像
(6) Design of student information management system
v-bind usage: class dynamic binding object array style style and function method
挖矿是什么意思?矿工都做了什么?
The CTF introductory notes of SQL injection
强化学习笔记:DDPG
OPENSSL基本实验以及OPENSSL详解
一次代码审计的笔记(CVE-2018-12613 phpmyadmin文件包含漏洞)
SATA M2 SSD 无法安装系统的解决方法
Cookie is used to collect the admin privileges CTF foundation problem
关于我的大创、论文~
How to calculate the distance between two points on the earth (with formula derivation)
Debian 12 Bookworm 尝鲜记
A code audit notes (CVE - 2018-12613 phpmyadmin file contains loopholes)
The CTF introduction of PHP file contains
2022年中高级 Android 大厂面试秘籍,为你保驾护航金九银十,直通大厂
file contains vulnerabilities
会计账簿、会计账簿概述、会计账簿的启用与登记要求、会计账簿的格式和登记方法
Alibaba Cloud MySQL 5.7 installation and some major problems (total)
不懂“赚钱逻辑”,你永远都是社会最底层(广告电商)