From 25d2b13aaa48992cf3f58c91d117f5a2ca951b68 Mon Sep 17 00:00:00 2001 From: Tanishmoral11 Date: Fri, 16 Aug 2024 15:56:19 +0530 Subject: [PATCH] version 2.1 --- .../example/chantingapp/BeadProgressView.kt | 68 +++++++++++++++++++ .../com/example/chantingapp/MainActivity.kt | 33 +++------ .../res/drawable/circular_progress_bar.xml | 4 +- .../main/res/drawable/count_background.xml | 9 +++ app/src/main/res/layout/activity_main.xml | 61 +++++++++-------- app/src/main/res/values/colors.xml | 2 + 6 files changed, 120 insertions(+), 57 deletions(-) create mode 100644 app/src/main/java/com/example/chantingapp/BeadProgressView.kt create mode 100644 app/src/main/res/drawable/count_background.xml diff --git a/app/src/main/java/com/example/chantingapp/BeadProgressView.kt b/app/src/main/java/com/example/chantingapp/BeadProgressView.kt new file mode 100644 index 0000000..6b5e29d --- /dev/null +++ b/app/src/main/java/com/example/chantingapp/BeadProgressView.kt @@ -0,0 +1,68 @@ +package com.example.chantingapp + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Paint +import android.util.AttributeSet +import android.view.View +import androidx.core.content.ContextCompat +import kotlin.math.cos +import kotlin.math.min +import kotlin.math.sin + +class BeadProgressView @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +) : View(context, attrs, defStyleAttr) { + + private val paint = Paint(Paint.ANTI_ALIAS_FLAG) + private val totalBeads = 108 + private var filledBeads = 0 + + init { + paint.style = Paint.Style.FILL + } + + fun setProgress(progress: Int) { + filledBeads = progress + invalidate() + } + + override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) + + val centerX = width / 2f + val centerY = height / 2f + val radius = 160f // Slightly smaller to fit within the view + + // Draw white background circle + paint.color = ContextCompat.getColor(context, android.R.color.white) + canvas.drawCircle(centerX, centerY, radius, paint) + + // Draw border + paint.style = Paint.Style.STROKE + paint.color = ContextCompat.getColor(context, android.R.color.darker_gray) + paint.strokeWidth = 2f + canvas.drawCircle(centerX, centerY, radius, paint) + + paint.style = Paint.Style.FILL + + val angleStep = 360f / totalBeads + val beadRadius = 10f // Smaller bead size + + for (i in 0 until totalBeads) { + val angle = Math.toRadians((-90 + i * angleStep).toDouble()) // Start from 12 o'clock + val x = (centerX + radius * 0.85 * cos(angle)).toFloat() // Move beads slightly inward + val y = (centerY + radius * 0.85 * sin(angle)).toFloat() // Move beads slightly inward + + paint.color = if (i < filledBeads) { + ContextCompat.getColor(context, R.color.bead_filled) + } else { + ContextCompat.getColor(context, R.color.bead_empty) + } + + canvas.drawCircle(x, y, beadRadius, paint) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/chantingapp/MainActivity.kt b/app/src/main/java/com/example/chantingapp/MainActivity.kt index f704e0f..85de034 100644 --- a/app/src/main/java/com/example/chantingapp/MainActivity.kt +++ b/app/src/main/java/com/example/chantingapp/MainActivity.kt @@ -1,31 +1,26 @@ package com.example.chantingapp -import android.animation.ObjectAnimator import android.content.Context import android.content.SharedPreferences import android.media.MediaPlayer import android.os.Bundle -import android.view.animation.DecelerateInterpolator import android.widget.Button -import android.widget.ProgressBar import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.viewpager.widget.ViewPager import java.text.SimpleDateFormat -import java.util.Calendar -import java.util.Locale +import java.util.* class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickListener { private lateinit var sharedPreferences: SharedPreferences - private lateinit var countTextView: TextView private lateinit var roundsTextView: TextView private lateinit var streakTextView: TextView private lateinit var mediaPlayer: MediaPlayer private lateinit var viewPager: ViewPager private lateinit var adapter: BackgroundPagerAdapter - private lateinit var progressBar: ProgressBar + private lateinit var beadProgressView: BeadProgressView private var count = 0 private var rounds = 0 @@ -42,15 +37,12 @@ class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickLis super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - progressBar = findViewById(R.id.progressBar) - progressBar.max = 108 - progressBar.progress = 0 - sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE) countTextView = findViewById(R.id.countTextView) roundsTextView = findViewById(R.id.roundsTextView) streakTextView = findViewById(R.id.streakTextView) + beadProgressView = findViewById(R.id.beadProgressView) val resetButton: Button = findViewById(R.id.resetButton) viewPager = findViewById(R.id.viewPager) @@ -62,7 +54,7 @@ class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickLis resetButton.setOnClickListener { count = 0 rounds = 0 - progressBar.progress = 0 + beadProgressView.setProgress(0) updateCount() } @@ -83,25 +75,18 @@ class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickLis override fun onImageClick(position: Int) { count++ - animateProgressBar(count) + beadProgressView.setProgress(count) if (count == 108) { rounds++ count = 0 - animateProgressBar(0) + beadProgressView.setProgress(0) updateStreak() } updateCount() } - private fun animateProgressBar(newProgress: Int) { - val animator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.progress, newProgress) - animator.duration = 300 // Duration of the animation in milliseconds - animator.interpolator = DecelerateInterpolator() - animator.start() - } - private fun updateCount() { if (count == 0 && rounds > 0) { mediaPlayer.start() @@ -109,9 +94,6 @@ class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickLis countTextView.text = count.toString() roundsTextView.text = rounds.toString() streakTextView.text = "Streak: $streak" - // Update progress percentage - val progressPercentage = (count.toFloat() / 108 * 100).toInt() - progressBar.contentDescription = "$progressPercentage% completed" } private fun updateStreak() { @@ -141,6 +123,7 @@ class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickLis count = sharedPreferences.getInt("count", 0) rounds = sharedPreferences.getInt("rounds", 0) streak = sharedPreferences.getInt("streak", 0) + beadProgressView.setProgress(count) } private fun saveCounts() { @@ -179,4 +162,4 @@ class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickLis super.onDestroy() mediaPlayer.release() } -} +} \ No newline at end of file diff --git a/app/src/main/res/drawable/circular_progress_bar.xml b/app/src/main/res/drawable/circular_progress_bar.xml index ba48f2f..d96587b 100644 --- a/app/src/main/res/drawable/circular_progress_bar.xml +++ b/app/src/main/res/drawable/circular_progress_bar.xml @@ -3,7 +3,7 @@ @@ -14,7 +14,7 @@ android:toDegrees="270"> diff --git a/app/src/main/res/drawable/count_background.xml b/app/src/main/res/drawable/count_background.xml new file mode 100644 index 0000000..7b3305d --- /dev/null +++ b/app/src/main/res/drawable/count_background.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f95968f..4adaba1 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -30,41 +30,42 @@ android:textSize="16sp" /> + - + + - + + + - - + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index b6da871..4465a07 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -5,6 +5,8 @@ #FFFFFFFF #674FA5 #FF0000 + #FF4081 + #FFFFFFFF \ No newline at end of file