1+ package com.aranandroid.customview.scrollview
2+
3+ import android.animation.ObjectAnimator
4+ import android.content.Context
5+ import android.graphics.Path
6+ import android.os.Build
7+ import android.util.AttributeSet
8+ import android.view.MotionEvent
9+ import android.widget.LinearLayout
10+ import androidx.annotation.Nullable
11+ import androidx.annotation.RequiresApi
12+
13+
14+ class ScrollLinearLayout (
15+ @Nullable context : Context ? ,
16+ @Nullable attrs : AttributeSet ? ,
17+ defStyleAttr : Int
18+ ) :
19+ LinearLayout (context!! , attrs, defStyleAttr) {
20+
21+
22+ private var lastX = 0
23+ private var lastY = 0
24+
25+ @RequiresApi(api = Build .VERSION_CODES .LOLLIPOP )
26+ override fun onTouchEvent (event : MotionEvent ): Boolean {
27+ val x = event.x.toInt()
28+ val y = event.y.toInt()
29+ when (event.action) {
30+ MotionEvent .ACTION_DOWN -> {
31+ lastX = x
32+ lastY = y
33+ }
34+ MotionEvent .ACTION_UP -> {
35+ val offX: Int = x - lastX
36+ val offY: Int = y - lastY
37+ animationScroll(getX() + offX, getY() + offY)
38+ }
39+ }
40+ return true
41+ }
42+
43+ @RequiresApi(api = Build .VERSION_CODES .LOLLIPOP )
44+ fun animationScroll (dx : Float , dy : Float ) {
45+ val path = Path ()
46+ path.moveTo(dx, dy)
47+ val objectAnimator = ObjectAnimator .ofFloat(this , " x" , " y" , path)
48+ objectAnimator.start()
49+ }
50+ }
0 commit comments