Skip to content

Commit

Permalink
added circular loading custom view like mcoc website and added that i…
Browse files Browse the repository at this point in the history
…nto a renamed module.
  • Loading branch information
apprajapati9 committed Sep 29, 2023
1 parent ba0c0f4 commit 36feea6
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 50 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ android {
dependencies {

implementation(project(":snowfall"))
implementation(project(":circlePoints"))
implementation(project(":customLoadingViews"))
implementation ("androidx.core:core-ktx:1.12.0")
implementation ("androidx.appcompat:appcompat:1.6.1")
implementation ("com.google.android.material:material:1.9.0")
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/res/layout/fragment_progress_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.apprajapati.myanimations.ui.fragments.progressview.ProgressCustomView
android:layout_width="match_parent"
android:layout_height="match_parent"
<!-- <com.apprajapati.myanimations.ui.fragments.progressview.ProgressCustomView-->
<!-- android:id="@+id/progressCustomView"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- app:layout_constraintBottom_toBottomOf="parent"-->
<!-- app:layout_constraintEnd_toEndOf="parent"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:layout_constraintTop_toTopOf="parent" />-->

<com.apprajapati.loadingviews.CircularLoadingView
android:layout_width="60dp"
android:layout_height="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down

This file was deleted.

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ plugins {


android {
namespace = "com.apprajapati.circlepoints"
namespace = "com.apprajapati.loadingviews"
compileSdk = 34

defaultConfig {
minSdk = 24
minSdk = 21
}

buildTypes {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.apprajapati.circlepoints">
package="com.apprajapati.loadingviews">

<application />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.apprajapati.circlepoints
package com.apprajapati.loadingviews

import android.content.Context
import android.graphics.Canvas
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.apprajapati.loadingviews

import android.animation.PropertyValuesHolder
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PointF
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.animation.DecelerateInterpolator

class CircularLoadingView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet), Runnable{

private val centerXY= PointF(0f, 0f)
private val mCirclePaint = Paint().apply{
isAntiAlias = true
style = Paint.Style.STROKE //stroke will make one layer only, fill will draw a filled circle.
color = Color.BLACK
strokeWidth = 10f
}

var minHeight = 0f //Minimum height and width of the view.
get() = field
set(value) {
field = value //value can only be changed when that's above 100.
}

val viewRect: RectF = RectF()
var radius = 0f
var outerCircleRadius = 0f

var valueAnimator = ValueAnimator()
var fadeAnimator = ValueAnimator()

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
//centerXY.getCenterXY() //at this point in this method doesn't give the correct center points.
//viewRect.set(centerXY.x, centerXY.y, minHeight, minHeight)
outerCircleRadius = minHeight
}

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
//Always pick the minimum value as a radius so full circle can be seen.
if(w < h){
minHeight = w.toFloat().div(2)-5
}else{
minHeight = h.toFloat().div(2)-5
}

if(w == h){
minHeight = w.toFloat().div(2)-5
}

mCirclePaint.strokeWidth = minHeight/8
animateProgress()
}

override fun onDraw(canvas: Canvas) {
centerXY.getCenterXY()

canvas.drawCircle(centerXY.x, centerXY.y, radius, mCirclePaint)
}

fun PointF.getCenterXY(){
x = width.toFloat().div(2) //width/2).toFloat()
y = height.toFloat().div(2) //height/2).toFloat()
}

fun animateProgress(){
val valuesHolder= PropertyValuesHolder.ofFloat("progressValue", 0f, minHeight)

val colorValues = PropertyValuesHolder.ofInt("colorValues", 0, 255) //0 being black and 255 being white.

valueAnimator = ValueAnimator().apply {
setValues(valuesHolder, colorValues)
duration = 1200
repeatCount = ValueAnimator.INFINITE

interpolator = DecelerateInterpolator(2f) //10f does give some interesting effects

addUpdateListener {
val colors = it.getAnimatedValue("colorValues") as Int
mCirclePaint.color = Color.rgb(colors, colors, colors)

val percentage = it.getAnimatedValue("progressValue") as Float
radius = percentage

invalidate()
}
}

valueAnimator.start()
}


override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
valueAnimator.end()
}
override fun run() {
TODO("Not yet implemented")
}
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ dependencyResolutionManagement {
rootProject.name = "MyAnimations"
include(":app")
include(":snowfall")
include(":circlePoints")
include(":customLoadingViews")

0 comments on commit 36feea6

Please sign in to comment.