Advertisement
leptodon

GradientCircularProgressBar

Nov 29th, 2020 (edited)
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.34 KB | None | 0 0
  1. package icu.stsi.myapplication11
  2.  
  3. import android.animation.ObjectAnimator
  4. import android.animation.ValueAnimator
  5. import android.content.Context
  6. import android.graphics.*
  7. import android.util.AttributeSet
  8. import android.util.DisplayMetrics
  9. import android.util.Log
  10. import android.util.TypedValue
  11. import android.view.View
  12. import android.view.animation.LinearInterpolator
  13. import androidx.core.content.res.ResourcesCompat
  14. import java.lang.Exception
  15. import kotlin.properties.Delegates
  16.  
  17.  
  18. class GradientCircularProgressBar @JvmOverloads constructor(
  19.     context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
  20. ) : View(context, attrs, defStyleAttr) {
  21.  
  22.     companion object {
  23.         private const val BODY_STROKE_WIDTH: Int = 8
  24.         private const val GLOW_STROKE_WIDTH: Int = 8
  25.  
  26. //      private const val GLOW_STROKE_WIDTH: Int = BODY_STROKE_WIDTH * 3
  27. //      private const val PADDING: Int = GLOW_STROKE_WIDTH / 2
  28.  
  29.         private const val PADDING: Int = GLOW_STROKE_WIDTH / 2 + BODY_STROKE_WIDTH
  30.         private const val BODY_LENGTH: Int = 270
  31.         private const val NORMALIZED_GRADIENT_LENGTH: Float = BODY_LENGTH / 360f
  32.  
  33.         // Rotating CONST
  34.         private const val ROTATION_DURATION: Int = 2500
  35.         private const val ROTATION_LENGTH: Int = 360
  36.         private const val ROTATION_PROPERTY_NAME: String = "rotation"
  37.  
  38.  
  39.         private var mPaddingPx by Delegates.notNull<Float>()
  40.         private var mBodyStrokeWidthPx by Delegates.notNull<Float>()
  41.         private var mGlowStrokeWidthPx by Delegates.notNull<Float>()
  42.         private lateinit var mPaintBody: Paint
  43.         private lateinit var mPaintBackground: Paint
  44.         private lateinit var mPaintGlow: Paint
  45.         private lateinit var mRect: RectF
  46.         private lateinit var mBodyGradient: SweepGradient
  47.         private lateinit var mBodyGradientFromToColors: IntArray
  48.         private lateinit var mGradientFromToPositions: FloatArray
  49.  
  50.         private lateinit var mRotationAnimator: ObjectAnimator
  51.         private val rotation: Float = 0f
  52.         private var growBodyLength: Int = 0
  53.     }
  54.  
  55.     init {
  56.  
  57.         setLayerType(LAYER_TYPE_SOFTWARE, null)
  58.  
  59.         val backgroundColor: Int = ResourcesCompat.getColor(resources, R.color.neon_background, null)
  60.         val bodyColor: Int = ResourcesCompat.getColor(resources, R.color.neon_body, null)
  61.         val glowColor: Int = ResourcesCompat.getColor(resources, R.color.neon_body, null)
  62.         val displayMetrics: DisplayMetrics = resources.displayMetrics
  63.         mBodyStrokeWidthPx = TypedValue.applyDimension(
  64.             TypedValue.COMPLEX_UNIT_DIP,
  65.             BODY_STROKE_WIDTH.toFloat(),
  66.             displayMetrics
  67.         )
  68.         mPaddingPx = TypedValue.applyDimension(
  69.             TypedValue.COMPLEX_UNIT_DIP,
  70.             PADDING.toFloat(),
  71.             displayMetrics
  72.         )
  73.         mGlowStrokeWidthPx = TypedValue.applyDimension(
  74.             TypedValue.COMPLEX_UNIT_DIP,
  75.             GLOW_STROKE_WIDTH.toFloat(),
  76.             displayMetrics
  77.         )
  78.         mPaintBackground = Paint()
  79.         mPaintBackground.color = backgroundColor
  80.         mPaintBackground.strokeWidth = mBodyStrokeWidthPx
  81.         mPaintBackground.style = Paint.Style.STROKE
  82.  
  83.         mRect = RectF()
  84.         mPaintBody = Paint()
  85.         mPaintBody.isAntiAlias = true
  86.         mPaintBody.color = bodyColor
  87.         mPaintBody.strokeWidth = mBodyStrokeWidthPx
  88.         mPaintBody.style = Paint.Style.STROKE
  89.         mPaintBody.strokeJoin = Paint.Join.ROUND
  90.         mPaintBody.strokeCap = Paint.Cap.ROUND
  91.  
  92.         mPaintGlow = Paint()
  93.         mPaintGlow.set(mPaintBody)
  94.         mPaintGlow.color = glowColor
  95.         mPaintGlow.strokeWidth = mGlowStrokeWidthPx
  96.         mPaintGlow.maskFilter = BlurMaskFilter(mBodyStrokeWidthPx, BlurMaskFilter.Blur.OUTER)
  97.  
  98.  
  99.         mBodyGradientFromToColors = intArrayOf(Color.TRANSPARENT, bodyColor)
  100.         mGradientFromToPositions = floatArrayOf(0F, NORMALIZED_GRADIENT_LENGTH)
  101.  
  102.         mRotationAnimator = createRotationAnimator()
  103.         growBody()
  104.     }
  105.  
  106.     override fun onDraw(canvas: Canvas?) {
  107.         val left: Float = mPaddingPx
  108.         val top: Float = mPaddingPx
  109.         val right: Float = width - mPaddingPx
  110.         val bottom: Float = height - mPaddingPx
  111.         mRect.set(left, top, right, bottom)
  112.  
  113.         if (!mRotationAnimator.isStarted && growBodyLength == BODY_LENGTH) {
  114. //            && growBodyLength == BODY_LENGTH
  115.             mRotationAnimator.start()
  116.         }
  117.         canvas?.rotate(rotation, (width / 2).toFloat(), (height / 2).toFloat())
  118.  
  119. //        canvas?.drawArc(mRect, BODY_LENGTH-30.toFloat(), 30f, false, mPaintGlow)
  120.  
  121.         canvas?.drawArc(mRect, 0f, 360f, false, mPaintBackground)
  122.         canvas?.drawArc(mRect, 10f, growBodyLength.toFloat(), false, mPaintGlow)
  123.         canvas?.drawArc(mRect, 10f, growBodyLength.toFloat(), false, mPaintBody)
  124.         Log.d("onDraw", "START!!!")
  125.     }
  126.  
  127.     override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
  128.         super.onSizeChanged(w, h, oldw, oldh)
  129.         val centerX: Float = (width / 2).toFloat()
  130.         val centerY: Float = (height / 2).toFloat()
  131.         mBodyGradient =
  132.             SweepGradient(centerX, centerY, mBodyGradientFromToColors, mGradientFromToPositions)
  133.         mPaintBody.shader = mBodyGradient
  134.         mPaintGlow.shader = mBodyGradient
  135.     }
  136.  
  137. //    override fun setRotation(rotationAngle: Float) {
  138. ////        rotation = rotationAngle
  139. ////        invalidate()
  140. //    }
  141.  
  142.  
  143.     private fun growBody() {
  144.         Thread(Runnable {
  145.             kotlin.run {
  146.                 for (i in 30..BODY_LENGTH) {
  147.                     growBodyLength = i
  148.                     invalidate()
  149.                     try {
  150.                         Thread.sleep(8)
  151.                     } catch (e: Exception) {
  152.                         e.printStackTrace()
  153.                     }
  154.                 }
  155.             }
  156.         }).start()
  157.     }
  158.  
  159.     private fun createRotationAnimator(): ObjectAnimator {
  160.         val rotateAnimator: ObjectAnimator =
  161.             ObjectAnimator.ofFloat(this, ROTATION_PROPERTY_NAME, ROTATION_LENGTH.toFloat())
  162.         rotateAnimator.duration = ROTATION_DURATION.toLong()
  163.         rotateAnimator.interpolator = LinearInterpolator()
  164.         rotateAnimator.repeatMode = ValueAnimator.RESTART
  165.         rotateAnimator.repeatCount = ValueAnimator.INFINITE
  166.         return rotateAnimator
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement