简体   繁体   English

使用Touch事件监听器移动RelativeLayout可以使布局在移动时摆动

[英]Moving RelativeLayout with Touch event listener makes the Layout Wiggle while moving

I am trying to move a RelativeLayout view around using theTouch Listener. 我正在尝试使用TheTouch Listener来移动RelativeLayout视图。

The problem is that While the Layout is indeed moving it is wiggeling fast between to positions. 问题在于,尽管版式确实在移动,但它在各个位置之间快速摆动。 This is the full touch listener 这是全触摸监听器

    protected void touch(){
    Mover manmove = new Mover();

   Wall.setOnTouchListener(new View.OnTouchListener() {


       @Override
       public boolean onTouch(View view, MotionEvent motionEvent) {
           final int action = motionEvent.getAction();
           if(!isZooming()){

           switch (action & MotionEvent.ACTION_MASK) {
               //If The Screen is Tapped
               case (MotionEvent.ACTION_UP):
               case (MotionEvent.ACTION_DOWN):
                   //Setting the initial Touch point
                   setTouchDownX(motionEvent.getX());
                   setTouchDownY(motionEvent.getY());

                   break;

               case (MotionEvent.ACTION_MOVE):


                       float my = getaY() + (motionEvent.getY() - getTouchDownY());
                       console.log(my);

                      // manmove.moveY(my);



                   break;

           }

           }


           return true;
       }

    });

 }

I have created a Console class that logs info to my console. 我创建了一个控制台类,将信息记录到控制台。

the manmove.moveY(my)code is at the bottom. manmove.moveY(my)代码在底部。

if I (as done above) comment out the manmove.moveY(), as in dont run the method. 如果我(如上所述)将manmove.moveY()注释掉,则不要运行该方法。 Console.log outputs the following (Which are the desired coordinates). Console.log输出以下内容(这是所需的坐标)。

I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/Console(Float):: 0.0
I/Console(Float):: 0.0
I/Console(Float):: -9.876221
I/Console(Float):: -22.249512
I/Console(Float):: -39.085938
I/Console(Float):: -58.50659
I/Console(Float):: -82.34204
I/Console(Float):: -101.75574
I/Console(Float):: -122.02234
I/Console(Float):: -141.76233
I/Console(Float):: -159.39001
I/Console(Float):: -174.78223
I/Console(Float):: -189.00647
I/Console(Float):: -202.30786
I/Console(Float):: -215.42957
I/Console(Float):: -229.95398
I/Console(Float):: -244.98132
I/Console(Float):: -258.14148
I/Console(Float):: -272.5271

As you can see they are sequential, so the move should be smooth. 如您所见,它们是顺序的,因此移动应该很顺利。 but if I run manmove.moveY(my) 但是如果我运行manmove.moveY(my)

                       else{
                       //Allow Scrolling on FullView
                       float my = getaY() + (motionEvent.getY() - getTouchDownY());
                       console.log(my);

                       manmove.moveY(my);


                   }

I get this Output; 我得到这个输出;

I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/Console(Float):: 0.0
I/Console(Float):: 0.0
I/Console(Float):: 0.0
I/Console(Float):: -7.222168
I/Console(Float):: -86.498535
I/Console(Float):: -60.04187
I/Console(Float):: -125.71619
I/Console(Float):: -92.756226
I/Console(Float):: -152.55365
I/Console(Float):: -113.45459
I/Console(Float):: -178.72412
I/Console(Float):: -140.0473
I/Console(Float):: -206.31036
I/Console(Float):: -170.90485
I/Console(Float):: -222.82135
I/Console(Float):: -188.71191
I/Console(Float):: -234.86389

As you can see the output isnt in order. 如您所见,输出不是按顺序排列的。 As if there are two different events happening. 好像发生了两个不同的事件。 Why am I getting this result by just running the below method 为什么仅通过运行以下方法即可得到此结果

(Wall by the way is a RelativeLayout) (顺便说一下,墙壁是一个RelativeLayout)

Here's the moveY(my) method 这是moveY(my)方法

public class Mover extends Brain {
public Mover(){

}

public void moveX(float val){


   Wall.setX(val);

}
public void moveY(float val){

    Wall.setY(val);

}

Okay guys forget about it I solved it. 好的,忘了我解决了它。

All I had to do is setaY(my) to update the new position. 我所要做的就是setaY(my)更新新职位。

        float my = getaY() + (motionEvent.getY() - getTouchDownY());
                       console.log(my);

                       manmove.moveY(my);
                       setaX(my);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM