Moving drawable follow the finger

前端 未结 2 536
一生所求
一生所求 2021-01-07 11:23

I have class that extends View.

private class Draw2D extends View{
             public Draw2D(Context context) {
        super(context);
              }

            


        
2条回答
  •  被撕碎了的回忆
    2021-01-07 12:09

    You need to track the coordinates of your drawable so you can compare with the touch x, y coordinates.

    So basically:

    // can modify to adjust speed at which drawable moves
    int movementSpeed = 2;  
    
    if (drawableX < x)
      drawableX += movementSpeed;
    else if (drawableX > x)
      drawableX -= movementSpeed;
    
    if (drawableY < y)
      drawableY += movementSpeed;
    else if (drawableY > y)
      drawableY -= movementSpeed;
    

    Then need to redraw your drawable with the updated drawableX, drawableY coordinates.

提交回复
热议问题