Skip to content

Commit

Permalink
1.progressbar support show center progress text.
Browse files Browse the repository at this point in the history
2.optimize sample
  • Loading branch information
heyangyang committed Jun 16, 2021
1 parent 38f51d7 commit 3e22ce2
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

// implementation project(path: ':ios-progress-bar')
implementation 'com.github.hyy920109:IOSProgressBar:v1.0.0'
implementation project(path: ':ios-progress-bar')
// implementation 'com.github.hyy920109:IOSProgressBar:v1.0.0'
}
20 changes: 12 additions & 8 deletions app/src/main/java/com/hyy/iosprogressbar_demo/FirstFragment.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.hyy.iosprogressbar_demo

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatTextView
import com.hyy.iosprogressbar.IOSProgressBar

/**
* A simple [Fragment] subclass as the default destination in the navigation.
*/
class FirstFragment : Fragment() {

private val textSizes = arrayOf(12f, 16f, 20f, 24f, 28f)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand All @@ -24,17 +25,20 @@ class FirstFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

view.findViewById<IOSProgressBar>(R.id.ios_progress_bar_horizontal)
.setOnProgressChangeListener { iosProgressBar, progress, maxProgress, minProgress, actionUp ->
Log.d(TAG, "onProgressChangedListener: $progress actionUp-->$actionUp")
}
val iosProgressBarHorizontal =
view.findViewById<IOSProgressBar>(R.id.ios_progress_bar_horizontal)
val tvProgress = view.findViewById<AppCompatTextView>(R.id.tv_progress)
iosProgressBarHorizontal.setOnProgressChangeListener { iosProgressBar, progress, maxProgress, minProgress, actionUp ->
"Current Progress is $progress".also { tvProgress.text = it }
}

val tvTest = view.findViewById<AppCompatTextView>(R.id.tv_test)
view.findViewById<IOSProgressBar>(R.id.ios_progress_bar_vertical)
.setOnProgressChangeListener { iosProgressBar, progress, maxProgress, minProgress, actionUp ->
if (progress < 1) {
iosProgressBar.setProgress(1)
}
tvTest.textSize = textSizes[progress]
}

"Current Progress is ${iosProgressBarHorizontal.getProgress()}".also { tvProgress.text = it }
}

companion object {
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/java/com/hyy/iosprogressbar_demo/ViewUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.hyy.iosprogressbar_demo

import android.content.res.Resources
import android.util.TypedValue

val Float.dp
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this,
Resources.getSystem().displayMetrics
)

val Float.sp
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
this,
Resources.getSystem().displayMetrics
)

val Int.dp
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(),
Resources.getSystem().displayMetrics
).toInt()

val Int.sp
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
this.toFloat(),
Resources.getSystem().displayMetrics
).toInt()
25 changes: 23 additions & 2 deletions app/src/main/res/layout/fragment_first.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,44 @@
android:id="@+id/ios_progress_bar_horizontal"
android:layout_width="164dp"
android:layout_height="56dp"
app:ipb_conner_radius="12dp"
app:ipb_conner_radius="8dp"
app:ipb_progress_show_text="true"
app:ipb_progress_bar_orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="@dimen/fab_margin"/>

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/fab_margin"
app:layout_constraintTop_toBottomOf="@+id/ios_progress_bar_horizontal"
app:layout_constraintStart_toStartOf="@+id/ios_progress_bar_horizontal"
app:layout_constraintEnd_toEndOf="@+id/ios_progress_bar_horizontal" />

<com.hyy.iosprogressbar.IOSProgressBar
android:id="@+id/ios_progress_bar_vertical"
android:layout_width="56dp"
android:layout_height="164dp"
app:ipb_conner_radius="12dp"
app:ipb_conner_radius="8dp"
app:ipb_progress_max="4"
app:ipb_progress_show_divider="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm HYY"
android:textSize="12sp"
android:layout_marginStart="@dimen/fab_margin"
app:layout_constraintStart_toEndOf="@+id/ios_progress_bar_vertical"
app:layout_constraintTop_toTopOf="@+id/ios_progress_bar_vertical"
app:layout_constraintBottom_toBottomOf="@+id/ios_progress_bar_vertical"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typealias OnProgressChangedListener = (iosProgressBar: IOSProgressBar, progress:

class IOSProgressBar constructor(context: Context, attributeSet: AttributeSet?) :
View(context, attributeSet) {

//progress listener
private var onProgressChangedListener: OnProgressChangedListener? = null

Expand All @@ -28,6 +29,7 @@ class IOSProgressBar constructor(context: Context, attributeSet: AttributeSet?)

private var progressRect = RectF()
private var backgroundRect = Rect()
private var progressTextBoundRect = Rect()
private var viewBackgroundPath = Path()

//progress conner round radius
Expand All @@ -38,17 +40,26 @@ class IOSProgressBar constructor(context: Context, attributeSet: AttributeSet?)
private var progressColor = "#FF018786".toColorInt()
//define conner rect
private var connerRect = RectF()
//show progress text or not
private var showProgressText: Boolean

//define paints
private val progressPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.FILL
}
private val backgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.FILL
}
private val dividerPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
}
style = Paint.Style.FILL
}

private val backgroundPaint =
Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.FILL
}

private val dividerPaint =
Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
}

private val progressTextPaint =
Paint(Paint.ANTI_ALIAS_FLAG)

//some progress variable
private var maxProgress = 100
Expand Down Expand Up @@ -210,6 +221,23 @@ class IOSProgressBar constructor(context: Context, attributeSet: AttributeSet?)
dividerPaint.color = dividerColor

orientation = getInteger(R.styleable.IOSProgressBar_ipb_progress_bar_orientation, ORIENTATION_VERTICAL)

//progress text
showProgressText = getBoolean(
R.styleable.IOSProgressBar_ipb_progress_show_text,
false
)
val progressTextColor = getColor(
R.styleable.IOSProgressBar_ipb_progress_text_color,
ContextCompat.getColor(context, R.color.default_progress_text_color)
)
progressTextPaint.color = progressTextColor

val progressTextSize = getDimension(
R.styleable.IOSProgressBar_ipb_progress_text_size,
resources.getDimension(R.dimen.default_progress_text_size)
)
progressTextPaint.textSize = progressTextSize
}

typedArray.recycle()
Expand Down Expand Up @@ -264,6 +292,8 @@ class IOSProgressBar constructor(context: Context, attributeSet: AttributeSet?)
onProgressChangedListener?.invoke(this, progress, maxProgress, minProgress, false)
}

fun getProgress() = progress

fun setOnProgressChangeListener(onProgressChangedListener: OnProgressChangedListener) {
this.onProgressChangedListener = onProgressChangedListener
}
Expand Down Expand Up @@ -342,8 +372,22 @@ class IOSProgressBar constructor(context: Context, attributeSet: AttributeSet?)
}

}

if (showProgressText) {
val progressText = getProgressText()
progressTextPaint.getTextBounds(progressText, 0, progressText.length, progressTextBoundRect)
canvas.drawText(progressText, (viewWidth-progressTextBoundRect.width())/2f, getProgressTextBaseLine(), progressTextPaint)
}
}
}

private fun getProgressTextBaseLine(): Float{
val fontMetrics = progressTextPaint.fontMetrics
val fontHeight = fontMetrics.descent - fontMetrics.ascent
val baseLine = viewHeight - (viewHeight - fontHeight)/2 - fontMetrics.descent
return baseLine
}
private fun getProgressText() = "$progress%"

override fun onTouchEvent(event: MotionEvent?): Boolean {
if (isEnabled.not()) return false
Expand Down
4 changes: 3 additions & 1 deletion ios-progress-bar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<attr name="ipb_progress_show_divider" format="boolean"/>
<attr name="ipb_progress_divider_height" format="dimension"/>
<attr name="ipb_progress_divider_color" format="color"/>

<attr name="ipb_progress_show_text" format="boolean"/>
<attr name="ipb_progress_text_color" format="color"/>
<attr name="ipb_progress_text_size" format="dimension"/>
<attr name="ipb_progress_bar_orientation" format="enum">
<enum name="vertical" value="1" />
<enum name="horizontal" value="2"/>
Expand Down
1 change: 1 addition & 0 deletions ios-progress-bar/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<resources>
<color name="default_background_color">#C0C0C0</color>
<color name="default_progress_color">#00796B</color>
<color name="default_progress_text_color">#FF5722</color>
</resources>
2 changes: 2 additions & 0 deletions ios-progress-bar/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<resources>
<dimen name="default_conner_radius">8dp</dimen>
<dimen name="default_divider_height">.5dp</dimen>

<dimen name="default_progress_text_size">14sp</dimen>
</resources>

0 comments on commit 3e22ce2

Please sign in to comment.