2020年3月13日金曜日

Drag を実装する その1 : GestureDetector なし

Drag and scale | Android Developers (MotionEventCompat を使ってたりちょっと古い)を参考に変えたもの
  1. class SimpleDragView : FrameLayout {  
  2.   
  3.     constructor(context: Context) : super(context)  
  4.     constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)  
  5.     constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(  
  6.         context,  
  7.         attrs,  
  8.         defStyleAttr  
  9.     )  
  10.   
  11.     private var activePointerId = INVALID_POINTER_ID  
  12.     private var lastTouchX = 0f  
  13.     private var lastTouchY = 0f  
  14.   
  15.     private val targetView: View  
  16.   
  17.     init {  
  18.         val size = (100 * resources.displayMetrics.density).toInt()  
  19.         targetView = View(context).apply {  
  20.             layoutParams = LayoutParams(size, size)  
  21.             setBackgroundColor(Color.RED)  
  22.         }  
  23.         addView(targetView)  
  24.     }  
  25.   
  26.     override fun onTouchEvent(ev: MotionEvent): Boolean {  
  27.         when (ev.actionMasked) {  
  28.             MotionEvent.ACTION_DOWN -> {  
  29.                 val pointerIndex = ev.actionIndex  
  30.                 val x = ev.getX(pointerIndex)  
  31.                 val y = ev.getY(pointerIndex)  
  32.   
  33.                 if (!(x.toInt() in 0..width && y.toInt() in 0..height)) {  
  34.                     return false  
  35.                 }  
  36.   
  37.                 val left = targetView.translationX  
  38.                 val right = left + targetView.width  
  39.                 val top = targetView.translationY  
  40.                 val bottom = top + targetView.height  
  41.   
  42.                 if (!(x in left..right && y in top..bottom)) {  
  43.                     return false  
  44.                 }  
  45.   
  46.                 lastTouchX = x  
  47.                 lastTouchY = y  
  48.   
  49.                 activePointerId = ev.getPointerId(0)  
  50.             }  
  51.             MotionEvent.ACTION_MOVE -> {  
  52.                 if (activePointerId == INVALID_POINTER_ID) {  
  53.                     return false  
  54.                 }  
  55.   
  56.                 val pointerIndex = ev.findPointerIndex(activePointerId)  
  57.                 val x = ev.getX(pointerIndex)  
  58.                 val y = ev.getY(pointerIndex)  
  59.   
  60.                 if (!(x.toInt() in 0..width && y.toInt() in 0..height)) {  
  61.                     return false  
  62.                 }  
  63.   
  64.                 val diffX = x - lastTouchX  
  65.                 val diffY = y - lastTouchY  
  66.   
  67.                 targetView.translationX += diffX  
  68.                 targetView.translationY += diffY  
  69.   
  70.                 lastTouchX = x  
  71.                 lastTouchY = y  
  72.             }  
  73.             MotionEvent.ACTION_UP,  
  74.             MotionEvent.ACTION_CANCEL -> {  
  75.                 activePointerId = INVALID_POINTER_ID  
  76.             }  
  77.             MotionEvent.ACTION_POINTER_UP -> {  
  78.                 if (activePointerId == INVALID_POINTER_ID) {  
  79.                     return false  
  80.                 }  
  81.   
  82.                 val pointerIndex = ev.actionIndex  
  83.                 if (ev.getPointerId(pointerIndex) != activePointerId) {  
  84.                     return false  
  85.                 }  
  86.   
  87.                 val newPointerIndex = if (pointerIndex == 01 else 0  
  88.                 val x = ev.getX(newPointerIndex)  
  89.                 val y = ev.getY(newPointerIndex)  
  90.   
  91.                 if (!(x.toInt() in 0..width && y.toInt() in 0..height)) {  
  92.                     activePointerId = INVALID_POINTER_ID  
  93.                     return false  
  94.                 }  
  95.   
  96.                 lastTouchX = x  
  97.                 lastTouchY = y  
  98.   
  99.                 activePointerId = ev.getPointerId(newPointerIndex)  
  100.             }  
  101.         }  
  102.   
  103.         return true  
  104.     }  
  105. }  



0 件のコメント:

コメントを投稿