Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.github.cooliceman.glide.transform
- import android.content.Context
- import android.graphics.*
- import com.bumptech.glide.Glide
- import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
- import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
- import java.nio.ByteBuffer
- import java.security.MessageDigest
- /**
- * <pre>
- * author : cooliceman
- * desc : 根据需求,对图片定制指定的圆角
- </pre> *
- */
- class RoundCornersTransformation(context: Context, private val mRadius: Int, type: CornerType) :
- BitmapTransformation() {
- private val ID = "com.github.cooliceman.glide.transform.RoundedCorners"
- private val ID_BYTES = ID.toByteArray(CHARSET)
- private val mBitmapPool: BitmapPool = Glide.get(context).bitmapPool
- private val mDiameter: Int
- private var mCornerType = CornerType.ALL
- val id: String
- get() = "RoundedTransformation(radius=$mRadius, diameter=$mDiameter)"
- init {
- mCornerType = type
- mDiameter = 2 * mRadius
- }
- enum class CornerType {
- /** 所有角 */
- ALL,
- /** 左上 */
- LEFT_TOP,
- /** 左下 */
- LEFT_BOTTOM,
- /** 右上 */
- RIGHT_TOP,
- /** 右下 */
- RIGHT_BOTTOM,
- /** 左侧 */
- LEFT,
- /** 右侧 */
- RIGHT,
- /** 下侧 */
- BOTTOM,
- /** 上侧 */
- TOP
- }
- override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap {
- val width = toTransform.width
- val height = toTransform.height
- var bitmap: Bitmap? = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888)
- if (bitmap == null) {
- bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
- }
- val canvas = Canvas(bitmap!!)
- val paint = Paint()
- paint.isAntiAlias = true
- paint.shader = BitmapShader(toTransform, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
- drawRoundRect(canvas, paint, width.toFloat(), height.toFloat())
- return bitmap
- }
- override fun updateDiskCacheKey(messageDigest: MessageDigest) {
- messageDigest.update(ID_BYTES)
- val radiusData = ByteBuffer.allocate(4).putInt(mRadius).array()
- messageDigest.update(radiusData)
- val cornerType = ByteBuffer.allocate(4).putInt(mCornerType.ordinal).array()
- messageDigest.update(cornerType)
- }
- private fun drawRoundRect(canvas: Canvas, paint: Paint, width: Float, height: Float) {
- when (mCornerType) {
- CornerType.LEFT_TOP -> drawLeftTopCorner(canvas, paint, width, height)
- CornerType.LEFT_BOTTOM -> drawLeftBottomCorner(canvas, paint, width, height)
- CornerType.RIGHT_TOP -> drawRightTopCorner(canvas, paint, width, height)
- CornerType.RIGHT_BOTTOM -> drawRightBottomCorner(canvas, paint, width, height)
- CornerType.LEFT -> drawLeftCorner(canvas, paint, width, height)
- CornerType.RIGHT -> drawRightCorner(canvas, paint, width, height)
- CornerType.BOTTOM -> drawBottomCorner(canvas, paint, width, height)
- CornerType.TOP -> drawTopCorner(canvas, paint, width, height)
- CornerType.ALL -> canvas.drawRoundRect(
- RectF(0f, 0f, width, height),
- mRadius.toFloat(),
- mRadius.toFloat(),
- paint
- )
- }
- }
- /**
- * 画左上角
- */
- private fun drawLeftTopCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
- canvas.drawRect(RectF(mRadius.toFloat(), 0f, width, height), paint)
- canvas.drawRect(RectF(0f, mRadius.toFloat(), mRadius.toFloat(), height), paint)
- canvas.drawArc(RectF(0f, 0f, mDiameter.toFloat(), mDiameter.toFloat()), 180f, 90f, true, paint)
- }
- /**
- * 画左下角
- */
- private fun drawLeftBottomCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
- canvas.drawRect(RectF(0f, 0f, width, height - mRadius), paint)
- canvas.drawRect(RectF(mRadius.toFloat(), height - mRadius, width, height), paint)
- canvas.drawArc(RectF(0f, height - mDiameter, mDiameter.toFloat(), height), 90f, 90f, true, paint)
- }
- /**
- * 画右上角
- */
- private fun drawRightTopCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
- canvas.drawRect(RectF(0f, 0f, width - mRadius, height), paint)
- canvas.drawRect(RectF(width - mRadius, mRadius.toFloat(), width, height), paint)
- canvas.drawArc(RectF(width - mDiameter, 0f, width, mDiameter.toFloat()), 270f, 90f, true, paint)
- }
- /**
- * 画右下角
- */
- private fun drawRightBottomCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
- canvas.drawRect(RectF(0f, 0f, width, height - mRadius), paint)
- canvas.drawRect(RectF(0f, height - mRadius, width - mRadius, height), paint)
- canvas.drawArc(RectF(width - mDiameter, height - mDiameter, width, height), 0f, 90f, true, paint)
- }
- /**
- * 画左 角
- */
- private fun drawLeftCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
- canvas.drawRect(RectF(mRadius.toFloat(), 0f, width, height), paint)
- canvas.drawRect(RectF(0f, mRadius.toFloat(), mRadius.toFloat(), height - mRadius), paint)
- canvas.drawArc(RectF(0f, 0f, mDiameter.toFloat(), mDiameter.toFloat()), 180f, 90f, true, paint)
- canvas.drawArc(RectF(0f, height - mDiameter, mDiameter.toFloat(), height), 90f, 90f, true, paint)
- }
- /**
- * 画右角
- */
- private fun drawRightCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
- canvas.drawRect(RectF(0f, 0f, width - mRadius, height), paint)
- canvas.drawRect(RectF(width - mRadius, mRadius.toFloat(), width, height - mRadius), paint)
- canvas.drawArc(RectF(width - mDiameter, 0f, width, mDiameter.toFloat()), 270f, 90f, true, paint)
- canvas.drawArc(RectF(width - mDiameter, height - mDiameter, width, height), 0f, 90f, true, paint)
- }
- /**
- * 画上 角
- */
- private fun drawTopCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
- canvas.drawRect(RectF(0f, mRadius.toFloat(), width, height), paint)
- canvas.drawRect(RectF(mRadius.toFloat(), 0f, width - mRadius, mRadius.toFloat()), paint)
- canvas.drawArc(RectF(0f, 0f, mDiameter.toFloat(), mDiameter.toFloat()), 180f, 90f, true, paint)
- canvas.drawArc(RectF(width - mDiameter, 0f, width, mDiameter.toFloat()), 270f, 90f, true, paint)
- }
- /**
- * 画下 角
- */
- private fun drawBottomCorner(canvas: Canvas, paint: Paint, width: Float, height: Float) {
- canvas.drawRect(RectF(0f, 0f, width, height - mRadius), paint)
- canvas.drawRect(RectF(mRadius.toFloat(), height - mRadius, width - mRadius, height), paint)
- canvas.drawArc(RectF(0f, height - mDiameter, mDiameter.toFloat(), height), 90f, 90f, true, paint)
- canvas.drawArc(RectF(width - mDiameter, height - mDiameter, width, height), 0f, 90f, true, paint)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement