Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. package com.github.cooliceman.glide.transform
  2.  
  3. import android.content.Context
  4. import android.graphics.*
  5. import com.bumptech.glide.Glide
  6. import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
  7. import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
  8. import java.nio.ByteBuffer
  9. import java.security.MessageDigest
  10.  
  11.  
  12. /**
  13. * <pre>
  14. * author : cooliceman
  15. * desc : 根据需求,对图片定制指定的圆角
  16. </pre> *
  17. */
  18.  
  19. class RoundCornersTransformation(context: Context, private val mRadius: Int, type: CornerType) :
  20. BitmapTransformation() {
  21. private val ID = "com.github.cooliceman.glide.transform.RoundedCorners"
  22. private val ID_BYTES = ID.toByteArray(CHARSET)
  23.  
  24. private val mBitmapPool: BitmapPool = Glide.get(context).bitmapPool
  25. private val mDiameter: Int
  26. private var mCornerType = CornerType.ALL
  27.  
  28.  
  29. val id: String
  30. get() = "RoundedTransformation(radius=$mRadius, diameter=$mDiameter)"
  31.  
  32.  
  33. init {
  34. mCornerType = type
  35. mDiameter = 2 * mRadius
  36. }
  37.  
  38. enum class CornerType {
  39. /** 所有角 */
  40. ALL,
  41. /** 左上 */
  42. LEFT_TOP,
  43. /** 左下 */
  44. LEFT_BOTTOM,
  45. /** 右上 */
  46. RIGHT_TOP,
  47. /** 右下 */
  48. RIGHT_BOTTOM,
  49. /** 左侧 */
  50. LEFT,
  51. /** 右侧 */
  52. RIGHT,
  53. /** 下侧 */
  54. BOTTOM,
  55. /** 上侧 */
  56. TOP
  57. }
  58.  
  59. override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap {
  60.  
  61. val width = toTransform.width
  62. val height = toTransform.height
  63. var bitmap: Bitmap? = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888)
  64. if (bitmap == null) {
  65. bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
  66. }
  67. val canvas = Canvas(bitmap!!)
  68. val paint = Paint()
  69. paint.isAntiAlias = true
  70. paint.shader = BitmapShader(toTransform, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
  71. drawRoundRect(canvas, paint, width.toFloat(), height.toFloat())
  72. return bitmap
  73. }
  74.  
  75.  
  76. override fun updateDiskCacheKey(messageDigest: MessageDigest) {
  77. messageDigest.update(ID_BYTES)
  78.  
  79. val radiusData = ByteBuffer.allocate(4).putInt(mRadius).array()
  80. messageDigest.update(radiusData)
  81.  
  82. val cornerType = ByteBuffer.allocate(4).putInt(mCornerType.ordinal).array()
  83. messageDigest.update(cornerType)
  84. }
  85.  
  86. private fun drawRoundRect(canvas: Canvas, paint: Paint, width: Float, height: Float) {
  87. when (mCornerType) {
  88. CornerType.LEFT_TOP -> drawLeftTopCorner(canvas, paint, width, height)
  89. CornerType.LEFT_BOTTOM -> drawLeftBottomCorner(canvas, paint, width, height)
  90. CornerType.RIGHT_TOP -> drawRightTopCorner(canvas, paint, width, height)
  91. CornerType.RIGHT_BOTTOM -> drawRightBottomCorner(canvas, paint, width, height)
  92. CornerType.LEFT -> drawLeftCorner(canvas, paint, width, height)
  93. CornerType.RIGHT -> drawRightCorner(canvas, paint, width, height)
  94. CornerType.BOTTOM -> drawBottomCorner(canvas, paint, width, height)
  95. CornerType.TOP -> drawTopCorner(canvas, paint, width, height)
  96. CornerType.ALL -> canvas.drawRoundRect(
  97. RectF(0f, 0f, width, height),
  98. mRadius.toFloat(),
  99. mRadius.toFloat(),
  100. paint
  101. )
  102. }
  103. }
  104.  
  105.  
  106. /**
  107. * 画左上角
  108. */
  109. private fun drawLeftTopCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
  110. canvas.drawRect(RectF(mRadius.toFloat(), 0f, width, height), paint)
  111. canvas.drawRect(RectF(0f, mRadius.toFloat(), mRadius.toFloat(), height), paint)
  112. canvas.drawArc(RectF(0f, 0f, mDiameter.toFloat(), mDiameter.toFloat()), 180f, 90f, true, paint)
  113. }
  114.  
  115. /**
  116. * 画左下角
  117. */
  118. private fun drawLeftBottomCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
  119. canvas.drawRect(RectF(0f, 0f, width, height - mRadius), paint)
  120. canvas.drawRect(RectF(mRadius.toFloat(), height - mRadius, width, height), paint)
  121. canvas.drawArc(RectF(0f, height - mDiameter, mDiameter.toFloat(), height), 90f, 90f, true, paint)
  122. }
  123.  
  124. /**
  125. * 画右上角
  126. */
  127. private fun drawRightTopCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
  128. canvas.drawRect(RectF(0f, 0f, width - mRadius, height), paint)
  129. canvas.drawRect(RectF(width - mRadius, mRadius.toFloat(), width, height), paint)
  130. canvas.drawArc(RectF(width - mDiameter, 0f, width, mDiameter.toFloat()), 270f, 90f, true, paint)
  131. }
  132.  
  133. /**
  134. * 画右下角
  135. */
  136. private fun drawRightBottomCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
  137. canvas.drawRect(RectF(0f, 0f, width, height - mRadius), paint)
  138. canvas.drawRect(RectF(0f, height - mRadius, width - mRadius, height), paint)
  139. canvas.drawArc(RectF(width - mDiameter, height - mDiameter, width, height), 0f, 90f, true, paint)
  140. }
  141.  
  142. /**
  143. * 画左 角
  144. */
  145. private fun drawLeftCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
  146. canvas.drawRect(RectF(mRadius.toFloat(), 0f, width, height), paint)
  147. canvas.drawRect(RectF(0f, mRadius.toFloat(), mRadius.toFloat(), height - mRadius), paint)
  148. canvas.drawArc(RectF(0f, 0f, mDiameter.toFloat(), mDiameter.toFloat()), 180f, 90f, true, paint)
  149. canvas.drawArc(RectF(0f, height - mDiameter, mDiameter.toFloat(), height), 90f, 90f, true, paint)
  150. }
  151.  
  152. /**
  153. * 画右角
  154. */
  155. private fun drawRightCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
  156. canvas.drawRect(RectF(0f, 0f, width - mRadius, height), paint)
  157. canvas.drawRect(RectF(width - mRadius, mRadius.toFloat(), width, height - mRadius), paint)
  158. canvas.drawArc(RectF(width - mDiameter, 0f, width, mDiameter.toFloat()), 270f, 90f, true, paint)
  159. canvas.drawArc(RectF(width - mDiameter, height - mDiameter, width, height), 0f, 90f, true, paint)
  160. }
  161.  
  162. /**
  163. * 画上 角
  164. */
  165. private fun drawTopCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
  166. canvas.drawRect(RectF(0f, mRadius.toFloat(), width, height), paint)
  167. canvas.drawRect(RectF(mRadius.toFloat(), 0f, width - mRadius, mRadius.toFloat()), paint)
  168. canvas.drawArc(RectF(0f, 0f, mDiameter.toFloat(), mDiameter.toFloat()), 180f, 90f, true, paint)
  169. canvas.drawArc(RectF(width - mDiameter, 0f, width, mDiameter.toFloat()), 270f, 90f, true, paint)
  170. }
  171.  
  172. /**
  173. * 画下 角
  174. */
  175. private fun drawBottomCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
  176. canvas.drawRect(RectF(0f, 0f, width, height - mRadius), paint)
  177. canvas.drawRect(RectF(mRadius.toFloat(), height - mRadius, width - mRadius, height), paint)
  178. canvas.drawArc(RectF(0f, height - mDiameter, mDiameter.toFloat(), height), 90f, 90f, true, paint)
  179. canvas.drawArc(RectF(width - mDiameter, height - mDiameter, width, height), 0f, 90f, true, paint)
  180. }
  181.  
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement