当前位置:网站首页>【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
最后,欢迎讨论。
边栏推荐
- From function test to advanced automation test, I stayed up 7 days to sort out this 3000 word super complete learning guide [with network disk resources]
- Market prospect analysis and Research Report of beam combiner in 2022
- Synchronized locked objects
- [数据集]|无人机视角
- Composition and configuration of GPS Beidou clock synchronization (satellite time synchronization system) in power plant
- Several time synchronization methods of Beidou timing system (GPS timing equipment)
- 【CNN】|How much position information do convolutional neural networks encode?
- June 10, 2022: Captain Shu crosses a sweet potato field from north to South (m long from north to South and N wide from east to West). The sweet potato field is divided into 1x1 squares. He can start
- 开源项目 英雄联盟 之WPF
- Implementation of one-dimensional convolutional neural network CNN based on FPGA (V) data quantization (with code)
猜你喜欢
![[cnn]|differences between CNN and transformer](/img/ed/2b47387ba390a02c24bf9ff85b36e4.png)
[cnn]|differences between CNN and transformer

Detailed explanation of network time synchronization (NTP network timing) of video monitoring system

如何提高回归测试效率

ETH 中继接口

【SignalR全套系列】之在.Net6中实SignalR通信

Quartz:老而弥坚的开源任务调度框架,用起来够丝滑

1_ Attribute management function

Exam item management system based on SSM

三层带防护内网红队靶场

编程大作战 -- 挑战高考题
随机推荐
基于FPGA的一维卷积神经网络CNN的实现(五)数据量化(附代码)
Discussion on the development trend of remote power management unit (Intelligent PDU)
高考结束,可以干什么事情,专业应该如何选择?-- 来自一个在校大学生的肺腑之言
JMeter piezometric interface programming North
Kirin V10 installation of tongweb7.0
7. 列表标签
Student online education and teaching course management system based on SSM framework
Esp32 porting lvgl
A.前缀极差(C语言)
Synchronized locked objects
Market prospect analysis and Research Report of programmable digital temperature regulator in 2022
B - wall painting (C language)
Go failing - expected ‘package‘, found ‘EOF‘
Manual testing cannot be changed to automated testing. What is missing?
Detailed explanation of scenario method for common test case design methods
合理使用线程池以及线程变量
关于重复发包的防护与绕过
Student teacher examination management system based on SSM framework
Quartz:老而弥坚的开源任务调度框架,用起来够丝滑
Eth Transfer