diff --git a/app/build.gradle b/app/build.gradle index ba4f9ff..ad82065 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/main/java/com/hyy/iosprogressbar_demo/FirstFragment.kt b/app/src/main/java/com/hyy/iosprogressbar_demo/FirstFragment.kt index aadf6e8..75fef27 100644 --- a/app/src/main/java/com/hyy/iosprogressbar_demo/FirstFragment.kt +++ b/app/src/main/java/com/hyy/iosprogressbar_demo/FirstFragment.kt @@ -1,11 +1,11 @@ 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 /** @@ -13,6 +13,7 @@ import com.hyy.iosprogressbar.IOSProgressBar */ class FirstFragment : Fragment() { + private val textSizes = arrayOf(12f, 16f, 20f, 24f, 28f) override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? @@ -24,17 +25,20 @@ class FirstFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - view.findViewById(R.id.ios_progress_bar_horizontal) - .setOnProgressChangeListener { iosProgressBar, progress, maxProgress, minProgress, actionUp -> - Log.d(TAG, "onProgressChangedListener: $progress actionUp-->$actionUp") - } + val iosProgressBarHorizontal = + view.findViewById(R.id.ios_progress_bar_horizontal) + val tvProgress = view.findViewById(R.id.tv_progress) + iosProgressBarHorizontal.setOnProgressChangeListener { iosProgressBar, progress, maxProgress, minProgress, actionUp -> + "Current Progress is $progress".also { tvProgress.text = it } + } + val tvTest = view.findViewById(R.id.tv_test) view.findViewById(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 { diff --git a/app/src/main/java/com/hyy/iosprogressbar_demo/ViewUtil.kt b/app/src/main/java/com/hyy/iosprogressbar_demo/ViewUtil.kt new file mode 100644 index 0000000..08affea --- /dev/null +++ b/app/src/main/java/com/hyy/iosprogressbar_demo/ViewUtil.kt @@ -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() \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_first.xml b/app/src/main/res/layout/fragment_first.xml index 463a85d..60032ec 100644 --- a/app/src/main/res/layout/fragment_first.xml +++ b/app/src/main/res/layout/fragment_first.xml @@ -10,18 +10,28 @@ 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"/> + + + + \ No newline at end of file diff --git a/ios-progress-bar/src/main/java/com/hyy/iosprogressbar/IOSProgressBar.kt b/ios-progress-bar/src/main/java/com/hyy/iosprogressbar/IOSProgressBar.kt index b968980..5797dc3 100644 --- a/ios-progress-bar/src/main/java/com/hyy/iosprogressbar/IOSProgressBar.kt +++ b/ios-progress-bar/src/main/java/com/hyy/iosprogressbar/IOSProgressBar.kt @@ -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 @@ -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 @@ -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 @@ -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() @@ -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 } @@ -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 diff --git a/ios-progress-bar/src/main/res/values/attrs.xml b/ios-progress-bar/src/main/res/values/attrs.xml index b391dd0..8e6592b 100644 --- a/ios-progress-bar/src/main/res/values/attrs.xml +++ b/ios-progress-bar/src/main/res/values/attrs.xml @@ -10,7 +10,9 @@ - + + + diff --git a/ios-progress-bar/src/main/res/values/colors.xml b/ios-progress-bar/src/main/res/values/colors.xml index 2b74c02..a13a1e3 100644 --- a/ios-progress-bar/src/main/res/values/colors.xml +++ b/ios-progress-bar/src/main/res/values/colors.xml @@ -2,4 +2,5 @@ #C0C0C0 #00796B + #FF5722 \ No newline at end of file diff --git a/ios-progress-bar/src/main/res/values/dimens.xml b/ios-progress-bar/src/main/res/values/dimens.xml index a1a1080..d06a8d7 100644 --- a/ios-progress-bar/src/main/res/values/dimens.xml +++ b/ios-progress-bar/src/main/res/values/dimens.xml @@ -2,4 +2,6 @@ 8dp .5dp + + 14sp \ No newline at end of file