Skip to content

Commit

Permalink
added loader
Browse files Browse the repository at this point in the history
  • Loading branch information
agrawalsuneet committed Jan 18, 2018
1 parent 404b6b0 commit 4ea7789
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 2 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_music_loader)
setContentView(R.layout.activity_main_rotating_square_loader)

supportActionBar?.title = "MusicPlayerLoader"
supportActionBar?.title = "RotatingSquareLoader"

container = findViewById(R.id.container)

Expand Down
69 changes: 69 additions & 0 deletions app/src/main/res/layout/activity_main_rotating_square_loader.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="center"
android:orientation="horizontal">


<!--<com.agrawalsuneet.squareloaderspack.loaders.MusicPlayerLoader
android:layout_width="match_parent"
android:layout_height="match_parent"
app:musicplayer_animDuration="500"
app:musicplayer_delayDuration="200"
app:musicplayer_interpolator="@android:anim/linear_interpolator"
app:musicplayer_isSingleColor="true"
app:musicplayer_noOfDots="5"
app:musicplayer_rectColor="@color/red"
app:musicplayer_rectDistance="2dp"
app:musicplayer_rectHeight="30dp"
app:musicplayer_rectWidth="8dp" />
<com.agrawalsuneet.squareloaderspack.loaders.MusicPlayerLoader
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
app:musicplayer_animDuration="500"
app:musicplayer_delayDuration="200"
app:musicplayer_interpolator="@android:anim/linear_interpolator"
app:musicplayer_isSingleColor="true"
app:musicplayer_noOfDots="5"
app:musicplayer_rectColor="@color/amber"
app:musicplayer_rectDistance="2dp"
app:musicplayer_rectHeight="30dp"
app:musicplayer_rectWidth="8dp" />
<com.agrawalsuneet.squareloaderspack.loaders.MusicPlayerLoader
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
app:musicplayer_animDuration="500"
app:musicplayer_delayDuration="200"
app:musicplayer_interpolator="@android:anim/linear_interpolator"
app:musicplayer_isSingleColor="true"
app:musicplayer_noOfDots="5"
app:musicplayer_rectColor="@color/green"
app:musicplayer_rectDistance="2dp"
app:musicplayer_rectHeight="30dp"
app:musicplayer_rectWidth="8dp" />-->

</LinearLayout>

<!--<com.agrawalsuneet.squareloaderspack.basicviews.SquareView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />-->


<com.agrawalsuneet.squareloaderspack.loaders.RotatingSquareLoader
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class SquareView : View, LoaderContract {
private var squareColor: Int = resources.getColor(R.color.grey)
private lateinit var squarePaint: Paint

private var isHollowSquare: Boolean = false
private var strokeWidth : Int = 50

constructor(context: Context) : super(context) {}

constructor(context: Context, squareColor: Int, length: Int) : super(context) {
Expand All @@ -24,6 +27,14 @@ class SquareView : View, LoaderContract {
initValues()
}

constructor(context: Context, squareColor: Int, length: Int, isHollow: Boolean, strokeWidth : Int) : super(context) {
this.squareColor = squareColor
this.squareLength = length
this.isHollowSquare = isHollow
this.strokeWidth = strokeWidth
initValues()
}

constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
initAttributes(attrs!!)
}
Expand All @@ -46,6 +57,10 @@ class SquareView : View, LoaderContract {
private fun initValues() {
squarePaint = Paint()
squarePaint.color = squareColor
if (isHollowSquare) {
squarePaint.style = Paint.Style.STROKE
squarePaint.strokeWidth = strokeWidth.toFloat()
}
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.agrawalsuneet.squareloaderspack.loaders

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.ViewTreeObserver
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import android.widget.LinearLayout
import com.agrawalsuneet.squareloaderspack.R
import com.agrawalsuneet.squareloaderspack.basicviews.LoaderContract
import com.agrawalsuneet.squareloaderspack.basicviews.SquareView

/**
* Created by suneet on 1/18/18.
*/
class RotatingSquareLoader : LinearLayout, LoaderContract {

var squareSideLenght: Float = 200.0f
var strokeWidth: Float = 50.0f

var squareColor: Int = resources.getColor(R.color.green)

var animDuration: Int = 2000

private lateinit var squareView: SquareView


constructor(context: Context) : super(context) {
initView()
}

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
initAttributes(attrs)
initView()
}

constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
initAttributes(attrs)
initView()
}

constructor(context: Context, noOfSticks: Int, outerCircleRadius: Float, innerCircleRadius: Float, sticksColor: Int, viewBackgroundColor: Int) : super(context) {
/*this.noOfSticks = noOfSticks
this.outerCircleRadius = outerCircleRadius
this.innerCircleRadius = innerCircleRadius
this.sticksColor = sticksColor*/
initView()
}

override fun initAttributes(attrs: AttributeSet) {
/*val typedArray = context.obtainStyledAttributes(attrs, R.styleable.RotatingCircularSticksLoader, 0, 0)
this.noOfSticks = typedArray
.getInteger(R.styleable.RotatingCircularSticksLoader_rotatingsticks_noOfSticks, 50)
this.outerCircleRadius = typedArray
.getDimension(R.styleable.RotatingCircularSticksLoader_rotatingsticks_outerCircleRadius, 200.0f)
this.innerCircleRadius = typedArray
.getDimension(R.styleable.RotatingCircularSticksLoader_rotatingsticks_innerCircleRadius, 100.0f)
this.sticksColor = typedArray
.getColor(R.styleable.RotatingCircularSticksLoader_rotatingsticks_stickColor, resources.getColor(R.color.grey))
this.viewBackgroundColor = typedArray
.getColor(R.styleable.RotatingCircularSticksLoader_rotatingsticks_viewBackgroundColor, resources.getColor(android.R.color.white))
this.animDuration = typedArray
.getInteger(R.styleable.RotatingCircularSticksLoader_rotatingsticks_animDuration, 5000)
typedArray.recycle()*/
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
setMeasuredDimension(2 * squareSideLenght.toInt(), 2 * squareSideLenght.toInt())
}

private fun initView() {
removeAllViews()
removeAllViewsInLayout()

gravity = Gravity.CENTER

squareView = SquareView(context, squareColor, squareSideLenght.toInt(), true, strokeWidth.toInt())


addView(squareView)

val loaderView = this

viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
startLoading()

val vto = loaderView.viewTreeObserver
vto.removeOnGlobalLayoutListener(this)
}
})
}

private fun startLoading() {

val rotationAnim = getRotateAnimation()
squareView.startAnimation(rotationAnim)
}

private fun getRotateAnimation(): RotateAnimation {

val transAnim = RotateAnimation(0f, 360f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f)
transAnim.duration = animDuration.toLong()
transAnim.fillAfter = true
transAnim.repeatCount = Animation.INFINITE
transAnim.repeatMode = Animation.RESTART
transAnim.interpolator = LinearInterpolator()

return transAnim
}
}

0 comments on commit 4ea7789

Please sign in to comment.