当前位置:网站首页>【CustomView】Glide+BitmapTransformation 图片上下边框波浪处理(WaveTransformation)
【CustomView】Glide+BitmapTransformation 图片上下边框波浪处理(WaveTransformation)
2022-06-11 03:47:00 【最孤单的人】
### 用到Glide来显示图片,就会需要添加Transformation来处理图片的圆形、圆角 以及其他样式,Transformation有各种各样的样式,这里我们需要实现下图效果:

*该篇封装的Transformation继承于BitmapTransformation来支持Glide。
如何实现:
/**
* 使图片的上下边框呈波浪状
*
* @property waveCorners 波浪直径
*/
class WaveTransformation(private val waveCorners: Int) : BitmapTransformation() {
override fun updateDiskCacheKey(messageDigest: MessageDigest) {
messageDigest.update(ID_BYTES)
}
override fun transform(
pool: BitmapPool,
toTransform: Bitmap,
outWidth: Int,
outHeight: Int
): Bitmap {
val canvasBitmap = Bitmap.createBitmap(
toTransform.width,
toTransform.height,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(canvasBitmap) // 创建画板
val paint = Paint().apply {
isAntiAlias = true
shader = BitmapShader(toTransform, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
} // 创建画笔,画笔内容为该 Bitmap
// 计算可以分成多少个小波浪
val index = canvas.width / waveCorners
var rectLeft = 0f
try {
TransformationUtils.getBitmapDrawableLock().lock()
// 循环绘制
for (i in 0 .. index) {
val rect = RectF(
rectLeft,
0f,
(rectLeft + waveCorners).toInt().toFloat(),
canvas.height.toFloat()
)
canvas.drawRoundRect(rect, waveCorners / 2f, waveCorners / 2f, paint)
rectLeft = (rectLeft + waveCorners).toInt().toFloat()
}
// 判断是否需要执行最后一次绘制
if (rectLeft < canvas.width.toFloat()){
val rect = RectF(
rectLeft,
0f,
canvas.width.toFloat(),
canvas.height.toFloat()
)
canvas.drawRoundRect(rect, waveCorners / 2f, waveCorners / 2f, paint)
}
canvas.setBitmap(null)
} finally {
TransformationUtils.getBitmapDrawableLock().unlock()
}
return canvasBitmap
}
companion object {
private const val ID = "WaveTransformation"
private val ID_BYTES = ID.toByteArray(CHARSET)
}
}在Glide中使用:
val requestOptions =
RequestOptions().transform(CenterCrop(), WaveTransformation(25))
Glide.with(context)
.load(url)
.thumbnail(0.2f)
.apply(requestOptions)
.into(view)继续封装:
fun <T>ImageView.loadWaveCorner(url: T, waveCorners: Int) {
if (context != null) {
val requestOptions =
RequestOptions().transform(CenterCrop(), WaveTransformation(waveCorners))
loadImage(context, this, url, requestOptions)
}
}
private fun <T> loadImage(
context: Context,
view: ImageView,
url: T,
requestOptions: RequestOptions,
) {
Glide.with(context)
.load(url)
.thumbnail(0.2f)
.apply(requestOptions)
.into(view)
}一行代码调用:
iv.loadWaveCorner(url, 25)
参考:
Glide: https://github.com/bumptech/glide
Transformations:https://github.com/wasabeef/glide-transformations
最后,欢迎讨论。
边栏推荐
- Detailed explanation of scenario method for common test case design methods
- Large factory outsourcing or self research company? How to choose a job for a tester?
- Market prospect analysis and Research Report of digital line scan camera in 2022
- Some differences between people
- Benefits of declaring variables
- Market prospect analysis and Research Report of integrated scanner in 2022
- ImportError: DLL load failed while importing _ Sqlite3: the specified module could not be found
- SSLStrip 终极版 —— location 劫持
- 难忘时刻
- Lua removing elements from a loop in a list
猜你喜欢
![[cnn]|differences between CNN and transformer](/img/ed/2b47387ba390a02c24bf9ff85b36e4.png)
[cnn]|differences between CNN and transformer

华生·K的秘密日记

2022-06-10:薯队长从北向南穿过一片红薯地(南北长M,东西宽N),红薯地被划分为1x1的方格, 他可以从北边的任何一个格子出发,到达南边的任何一个格子, 但每一步只能走到东南、正南、西南方向的

VNC remote configuration of Galaxy Kirin server system

7. list label

Run Skynet for the first time

Typera+picgo+ common configurations of Tencent cloud cos

Eth relay interface

SSLStrip 终极版 —— location 劫持
![[CNN]|CNN与Transformer区别](/img/ed/2b47387ba390a02c24bf9ff85b36e4.png)
[CNN]|CNN与Transformer区别
随机推荐
Market prospect analysis and Research Report of beam combiner in 2022
Market prospect analysis and Research Report of hydrogen liquefier in 2022
7. list label
Code replicates CSRF attack and resolves it
Embedded basic interface-i2c
Management system of College Students' associations based on SSM
Makefile |
[CNN]|平移不变性
什麼樣的人才是幸福的?
Host computer development (how to develop host computer)
Thoughts on the number of threads and CPU caused by the CPU load high alarm
Eth Transfer
Some differences between people
Market prospect analysis and Research Report of surround packing machine in 2022
Eth relay interface
Vulkan-官方示例解读-Shadows(光栅化)
[network] socket programming
Implementation of one-dimensional convolutional neural network CNN based on FPGA (V) data quantization (with code)
This artifact is highly recommended. One line command will convert the web page to PDF!
Detailed explanation of network time synchronization (NTP network timing) of video monitoring system