当前位置:网站首页>【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
最后,欢迎讨论。
边栏推荐
- Synchronized locked objects
- 【CNN】|How much position information do convolutional neural networks encode?
- 什麼樣的人才是幸福的?
- How does the NTP clock server (satellite clock system) coordinate the integrated system?
- Market prospect analysis and Research Report of beam combiner in 2022
- Comparison of four delivery modes of Message Oriented Middleware
- Sentence s, paragraph P in VIM text object
- Market prospect analysis and Research Report of single photon counting detector in 2022
- 关于重复发包的防护与绕过
- 难忘时刻
猜你喜欢

Linq. pdf

Kirin V10 installation of tongweb7.0

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

JMeter piezometric interface programming North

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

NTP time server (GPS Beidou satellite synchronous clock) application boiler monitoring system
![[network] socket programming](/img/df/2afc300bfc2dd319247a4b75ef7e2c.png)
[network] socket programming

Docker swarm installing MySQL Cluster

Comparison of four delivery modes of Message Oriented Middleware

代码复现CSRF攻击并解决它
随机推荐
Market prospect analysis and Research Report of denitrification unit in 2022
Market prospect analysis and Research Report of digital line scan camera in 2022
Course classification tree structure display
A.前缀极差(C语言)
ETH 中继接口
合理使用线程池以及线程变量
[pan micro E9 development] single sign on Kingdee eas
1_ Attribute management function
常用测试用例设计方法之场景法详解
Record the problem of Galaxy Kirin V10 server version once: an error is reported when installing KVM
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 electronic pelletizing counter in 2022
GPS Beidou time service, NTP makes network clock synchronization more accurate
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
2022-06-10:薯队长从北向南穿过一片红薯地(南北长M,东西宽N),红薯地被划分为1x1的方格, 他可以从北边的任何一个格子出发,到达南边的任何一个格子, 但每一步只能走到东南、正南、西南方向的
Eth Of Erc20 And Erc721
【SignalR全套系列】之在.Net6中实SignalR通信
SSLStrip 终极版 —— location 劫持
什麼樣的人才是幸福的?
PMM monitoring Oracle