Skip to content

Commit

Permalink
preventing memory leak in customview
Browse files Browse the repository at this point in the history
  • Loading branch information
apprajapati9 committed Oct 1, 2023
1 parent 4bcd150 commit e913072
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package com.apprajapati.myanimations.ui.fragments.progressview

import android.os.Bundle
import android.view.View
import com.apprajapati.myanimations.databinding.FragmentProgressViewBinding
import com.apprajapati.myanimations.ui.BaseFragment

class ProgressViewFragment : BaseFragment<FragmentProgressViewBinding>(FragmentProgressViewBinding::inflate) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.hideShowButton.setOnClickListener {
if(binding.loadingView.visibility == View.VISIBLE){
binding.loadingView.visibility = View.GONE
}else{
binding.loadingView.visibility = View.VISIBLE
}
}
}
}
12 changes: 10 additions & 2 deletions app/src/main/res/layout/fragment_progress_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:layout_constraintTop_toTopOf="parent" />-->

<com.google.android.material.button.MaterialButton
android:id="@+id/hideShowButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="showHide button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.apprajapati.loadingviews.CircularLoadingView
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:duration="1000"
app:circleWidth="20"
android:id="@+id/loading_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class CircularLoadingView(context: Context, attributeSet: AttributeSet) :

private val handler = Handler(Looper.getMainLooper())

private var valueAnimator = ValueAnimator()

init {
val setValues =
context.obtainStyledAttributes(attributeSet, R.styleable.CircularLoadingView, 0, 0)
Expand Down Expand Up @@ -116,7 +118,30 @@ class CircularLoadingView(context: Context, attributeSet: AttributeSet) :
}
}

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
stopAnimation()
}

override fun setVisibility(visibility: Int) {
if(visibility == GONE || visibility == INVISIBLE){
stopAnimation()
}else{
animateProgress()
}
super.setVisibility(visibility)
}

fun stopAnimation(){
if(valueAnimator != null){
valueAnimator.cancel()
valueAnimator.end()
}
}


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

val colorValues = getColorRange() //0 being black and 255 being white.
Expand All @@ -125,7 +150,7 @@ class CircularLoadingView(context: Context, attributeSet: AttributeSet) :
shownOuter = true
}, 300)

val valueAnimator = ValueAnimator().apply {
valueAnimator = ValueAnimator().apply {
setValues(valuesHolder, colorValues)
duration = animationDuration.toLong()
repeatCount = ValueAnimator.INFINITE
Expand Down

0 comments on commit e913072

Please sign in to comment.