1+ package com.agrawalsuneet.squareloaderspack.loaders
2+
3+ import android.content.Context
4+ import android.util.AttributeSet
5+ import android.view.Gravity
6+ import android.view.ViewTreeObserver
7+ import android.view.animation.Animation
8+ import android.view.animation.LinearInterpolator
9+ import android.view.animation.RotateAnimation
10+ import android.widget.LinearLayout
11+ import com.agrawalsuneet.squareloaderspack.R
12+ import com.agrawalsuneet.squareloaderspack.basicviews.LoaderContract
13+ import com.agrawalsuneet.squareloaderspack.basicviews.SquareView
14+
15+ /* *
16+ * Created by suneet on 1/18/18.
17+ */
18+ class RotatingSquareLoader : LinearLayout , LoaderContract {
19+
20+ var squareSideLenght: Float = 200.0f
21+ var strokeWidth: Float = 50.0f
22+
23+ var squareColor: Int = resources.getColor(R .color.green)
24+
25+ var animDuration: Int = 2000
26+
27+ private lateinit var squareView: SquareView
28+
29+
30+ constructor (context: Context ) : super (context) {
31+ initView()
32+ }
33+
34+ constructor (context: Context , attrs: AttributeSet ) : super (context, attrs) {
35+ initAttributes(attrs)
36+ initView()
37+ }
38+
39+ constructor (context: Context , attrs: AttributeSet , defStyleAttr: Int ) : super (context, attrs, defStyleAttr) {
40+ initAttributes(attrs)
41+ initView()
42+ }
43+
44+ constructor (context: Context , noOfSticks: Int , outerCircleRadius: Float , innerCircleRadius: Float , sticksColor: Int , viewBackgroundColor: Int ) : super (context) {
45+ /* this.noOfSticks = noOfSticks
46+ this.outerCircleRadius = outerCircleRadius
47+ this.innerCircleRadius = innerCircleRadius
48+ this.sticksColor = sticksColor*/
49+ initView()
50+ }
51+
52+ override fun initAttributes (attrs : AttributeSet ) {
53+ /* val typedArray = context.obtainStyledAttributes(attrs, R.styleable.RotatingCircularSticksLoader, 0, 0)
54+
55+ this.noOfSticks = typedArray
56+ .getInteger(R.styleable.RotatingCircularSticksLoader_rotatingsticks_noOfSticks, 50)
57+
58+ this.outerCircleRadius = typedArray
59+ .getDimension(R.styleable.RotatingCircularSticksLoader_rotatingsticks_outerCircleRadius, 200.0f)
60+ this.innerCircleRadius = typedArray
61+ .getDimension(R.styleable.RotatingCircularSticksLoader_rotatingsticks_innerCircleRadius, 100.0f)
62+
63+
64+ this.sticksColor = typedArray
65+ .getColor(R.styleable.RotatingCircularSticksLoader_rotatingsticks_stickColor, resources.getColor(R.color.grey))
66+ this.viewBackgroundColor = typedArray
67+ .getColor(R.styleable.RotatingCircularSticksLoader_rotatingsticks_viewBackgroundColor, resources.getColor(android.R.color.white))
68+
69+ this.animDuration = typedArray
70+ .getInteger(R.styleable.RotatingCircularSticksLoader_rotatingsticks_animDuration, 5000)
71+
72+ typedArray.recycle()*/
73+ }
74+
75+ override fun onMeasure (widthMeasureSpec : Int , heightMeasureSpec : Int ) {
76+ super .onMeasure(widthMeasureSpec, heightMeasureSpec)
77+ setMeasuredDimension(2 * squareSideLenght.toInt(), 2 * squareSideLenght.toInt())
78+ }
79+
80+ private fun initView () {
81+ removeAllViews()
82+ removeAllViewsInLayout()
83+
84+ gravity = Gravity .CENTER
85+
86+ squareView = SquareView (context, squareColor, squareSideLenght.toInt(), true , strokeWidth.toInt())
87+
88+
89+ addView(squareView)
90+
91+ val loaderView = this
92+
93+ viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver .OnGlobalLayoutListener {
94+ override fun onGlobalLayout () {
95+ startLoading()
96+
97+ val vto = loaderView.viewTreeObserver
98+ vto.removeOnGlobalLayoutListener(this )
99+ }
100+ })
101+ }
102+
103+ private fun startLoading () {
104+
105+ val rotationAnim = getRotateAnimation()
106+ squareView.startAnimation(rotationAnim)
107+ }
108+
109+ private fun getRotateAnimation (): RotateAnimation {
110+
111+ val transAnim = RotateAnimation (0f , 360f ,
112+ Animation .RELATIVE_TO_SELF , 0.5f ,
113+ Animation .RELATIVE_TO_SELF , 0.5f )
114+ transAnim.duration = animDuration.toLong()
115+ transAnim.fillAfter = true
116+ transAnim.repeatCount = Animation .INFINITE
117+ transAnim.repeatMode = Animation .RESTART
118+ transAnim.interpolator = LinearInterpolator ()
119+
120+ return transAnim
121+ }
122+ }
0 commit comments