Skip to content

Commit 6ea2e8c

Browse files
jamshidjamshid
jamshid
authored and
jamshid
committed
Example with custom view
1 parent 0a0d8c6 commit 6ea2e8c

File tree

9 files changed

+127
-15
lines changed

9 files changed

+127
-15
lines changed

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ allprojects {
2020
repositories {
2121
google()
2222
jcenter()
23-
23+
maven { url 'https://jitpack.io' }
2424
}
2525
}
2626

library/src/main/java/uz/jamshid/library/IGRefreshLayout.kt

+23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package uz.jamshid.library
22

33
import android.content.Context
4+
import android.graphics.Color
45
import android.util.AttributeSet
56
import android.view.MotionEvent
67
import android.view.View
@@ -46,6 +47,9 @@ class IGRefreshLayout @JvmOverloads constructor(
4647
private var mBar: BaseProgressBar = CircleProgressBar(context)
4748
private var callback: InstaRefreshCallback?=null
4849
private var customViewSet = false
50+
private var customView: View?=null
51+
private var customViewHeight: Int = 0
52+
private var customViewWidth: Int = 0
4953

5054
init {
5155
mDecelerateInterpolator = DecelerateInterpolator(DECELERATE_INTERPOLATION_FACTOR)
@@ -54,6 +58,7 @@ class IGRefreshLayout @JvmOverloads constructor(
5458
setRefreshing(false)
5559
setupAttributes(attrs)
5660
mTotalDragDistance = dp2px(DRAG_MAX_DISTANCE)
61+
setBackgroundColor(Color.WHITE)
5762

5863
setWillNotDraw(false)
5964
ViewCompat.setChildrenDrawingOrderEnabled(this, true)
@@ -85,6 +90,13 @@ class IGRefreshLayout @JvmOverloads constructor(
8590
addView(mBar, 0)
8691
}
8792

93+
fun setCustomView(view: View, height: Int, width: Int){
94+
customView = view
95+
customViewHeight = height
96+
customViewWidth = width
97+
addView(view, 0)
98+
}
99+
88100
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
89101
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
90102

@@ -99,6 +111,8 @@ class IGRefreshLayout @JvmOverloads constructor(
99111

100112
mTarget?.measure(width, height)
101113
mBar.measure(width, height)
114+
if(customView!=null)
115+
customView?.measure(width, height)
102116
}
103117

104118
private fun ensureTarget(){
@@ -378,6 +392,15 @@ class IGRefreshLayout @JvmOverloads constructor(
378392

379393
mTarget?.layout(left, top + mCurrentOffsetTop, left + width - right, top + height - bottom + mCurrentOffsetTop)
380394
mBar.layout(left, top, left+width-right, top+height-bottom)
395+
if(customView!=null) {
396+
if(DRAG_MAX_DISTANCE - customViewHeight <= 0)
397+
customView?.layout(left, top+dp2px(30), left + width - right, customViewHeight+dp2px(30))
398+
else {
399+
val diff = DRAG_MAX_DISTANCE - customViewHeight
400+
val center = (width - customViewWidth)/2
401+
customView?.layout(center, diff/2, customViewWidth+center, DRAG_MAX_DISTANCE - diff/2)
402+
}
403+
}
381404
}
382405

383406
fun setRefreshListener(action:()->Unit){

library/src/main/java/uz/jamshid/library/progress_bar/BaseProgressBar.kt

+4
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ abstract class BaseProgressBar @JvmOverloads constructor(
1717
abstract fun setParent(parent: IGRefreshLayout)
1818
abstract fun start()
1919
abstract fun stop()
20+
21+
fun dp2px(dp: Int): Int{
22+
return dp*context.resources.displayMetrics.density.toInt()
23+
}
2024
}

library/src/main/java/uz/jamshid/library/progress_bar/CircleProgressBar.kt

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import android.graphics.Color
88
import android.graphics.Paint
99
import android.graphics.RectF
1010
import android.util.AttributeSet
11+
import android.view.animation.Animation
1112
import android.view.animation.LinearInterpolator
13+
import android.view.animation.ScaleAnimation
1214
import uz.jamshid.library.IGRefreshLayout
1315

1416

@@ -22,9 +24,9 @@ class CircleProgressBar @JvmOverloads constructor(
2224
private var frontColor = Color.GRAY
2325
private val backPaint = Paint(Paint.ANTI_ALIAS_FLAG)
2426

25-
private var borderWidth = 4.0f
27+
private var borderWidth = dp2px(4).toFloat()
2628

27-
private var size = (80 * context.resources.displayMetrics.density).toInt()
29+
private var size = dp2px(80)
2830
private var mIndeterminateSweep: Float = 0f
2931
private var mStartAngle: Float = 0f
3032

@@ -44,6 +46,11 @@ class CircleProgressBar @JvmOverloads constructor(
4446

4547
}
4648

49+
fun setBorderWidth(width: Int){
50+
paint.strokeWidth = dp2px(width).toFloat()
51+
backPaint.strokeWidth = dp2px(width).toFloat()
52+
}
53+
4754
fun setColors(backColor: Int, frontColor: Int){
4855
paint.color = frontColor
4956
backPaint.color = backColor

library/src/main/java/uz/jamshid/library/progress_bar/LineProgressBar.kt

+18-8
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class LineProgressBar @JvmOverloads constructor(
1919
private var backColor = Color.LTGRAY
2020
private var frontColor = Color.GRAY
2121
private val backPaint = Paint(Paint.ANTI_ALIAS_FLAG)
22-
private var borderWidth = 4.0f
23-
private val left = 10f
24-
private var top = 60f
22+
private var borderWidth = dp2px(4).toFloat()
23+
private var left = dp2px(10).toFloat()
24+
private var top = dp2px(60).toFloat()
2525
private var isLoaded = false
2626

2727
private var progressAnimator: ValueAnimator? = null
@@ -37,6 +37,16 @@ class LineProgressBar @JvmOverloads constructor(
3737
backPaint.strokeWidth = borderWidth
3838
}
3939

40+
fun setLeftAndTop(left: Int, top: Int){
41+
this.left = dp2px(left).toFloat()
42+
this.top = dp2px(top).toFloat()
43+
}
44+
45+
fun setBorderWidth(width: Int){
46+
paint.strokeWidth = dp2px(width).toFloat()
47+
backPaint.strokeWidth = dp2px(width).toFloat()
48+
}
49+
4050
fun setColors(backColor: Int, frontColor: Int){
4151
paint.color = frontColor
4252
backPaint.color = backColor
@@ -46,10 +56,10 @@ class LineProgressBar @JvmOverloads constructor(
4656
super.onDraw(canvas)
4757

4858
top = (mParent.DRAG_MAX_DISTANCE/2).toFloat()
49-
var currentPercent = (width - 10f)*(mPercent/100)
50-
val lineWidth = width - 10f
51-
if(currentPercent<=10)
52-
currentPercent = 10f
59+
var currentPercent = (width - left)*(mPercent/100)
60+
val lineWidth = width - left
61+
if(currentPercent<=left)
62+
currentPercent = left
5363

5464
canvas?.drawLine(left, top, lineWidth, top, backPaint)
5565

@@ -88,7 +98,7 @@ class LineProgressBar @JvmOverloads constructor(
8898
if(progressAnimator != null && progressAnimator!!.isRunning)
8999
progressAnimator?.cancel()
90100

91-
progressAnimator = ValueAnimator.ofFloat(10f, width.toFloat())
101+
progressAnimator = ValueAnimator.ofFloat(left, width.toFloat())
92102
progressAnimator?.duration = 500
93103
progressAnimator?.interpolator = LinearInterpolator()
94104
progressAnimator?.addUpdateListener {
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M15.5,9.5m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"/>
9+
<path
10+
android:fillColor="#FF000000"
11+
android:pathData="M8.5,9.5m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"/>
12+
<path
13+
android:fillColor="#FF000000"
14+
android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8zM12,16c-1.48,0 -2.75,-0.81 -3.45,-2L6.88,14c0.8,2.05 2.79,3.5 5.12,3.5s4.32,-1.45 5.12,-3.5h-1.67c-0.7,1.19 -1.97,2 -3.45,2z"/>
15+
</vector>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package uz.jamshid.igrefreshlayout
2+
3+
import android.content.Context
4+
import android.util.AttributeSet
5+
import android.view.animation.Animation
6+
import android.view.animation.RotateAnimation
7+
import android.widget.ImageView
8+
import uz.jamshid.library.IGRefreshLayout
9+
import uz.jamshid.library.progress_bar.BaseProgressBar
10+
11+
12+
class Circle @JvmOverloads constructor(
13+
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
14+
) : BaseProgressBar(context, attrs, defStyleAttr) {
15+
16+
var bar = ImageView(context)
17+
init {
18+
bar.setImageResource(R.drawable.ic_smile)
19+
bar.alpha = 0f
20+
}
21+
22+
override fun setParent(parent: IGRefreshLayout) {
23+
mParent = parent
24+
setUpView()
25+
}
26+
27+
override fun setPercent(percent: Float) {
28+
mPercent = percent
29+
bar.alpha = percent/100
30+
}
31+
32+
private fun setUpView(){
33+
mParent.setCustomView(bar, dp2px(80), dp2px(80))
34+
}
35+
36+
override fun start() {
37+
val animation1 = RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
38+
animation1.duration = 500
39+
animation1.repeatCount = Animation.INFINITE
40+
bar.startAnimation(animation1)
41+
}
42+
43+
override fun stop() {
44+
bar.alpha = 0f
45+
bar.clearAnimation()
46+
}
47+
}

sample/src/main/java/uz/jamshid/igrefreshlayout/MainActivity.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package uz.jamshid.igrefreshlayout
22

3-
import android.graphics.Color
43
import android.os.Bundle
54
import android.os.Handler
65
import androidx.appcompat.app.AppCompatActivity
@@ -26,10 +25,11 @@ class MainActivity : AppCompatActivity() {
2625
}, 3000)
2726
}
2827

29-
val cc = CircleProgressBar(this)
3028

29+
val cc = CircleProgressBar(this)
30+
cc.setSize(90)
3131
val l = LineProgressBar(this)
32-
l.setColors(Color.parseColor("#84ff9d"), Color.parseColor("#004500"))
33-
swipe.setCustomBar(l)
32+
// l.setColors(Color.parseColor("#84ff9d"), Color.parseColor("#004500"))
33+
swipe.setCustomBar(Circle(this))
3434
}
3535
}

0 commit comments

Comments
 (0)