Java Android:在一秒钟内添加阵列15次如何?

时间:2014-07-02 04:42:19

标签: java android arraylist handler

我想在一秒钟内添加15次数组。我使用过Timer,Countdowntimer,处理程序,但不能用于Button Action.Down和Action.Move。有什么帮助吗?

ArrayList<Integer> Xpos;
ArrayList<Integer> Ypos;
ArrayList<String> MarkerSelected;
private Handler handlerXYPos = new Handler();
Runnable runnableXYPos = null;



case MotionEvent.ACTION_DOWN:
handlerXYPos.post(runnableXYPos);   
break;

case MotionEvent.ACTION_UP:
handlerXYPos.removeCallbacks(runnableXYPos);
break;


//=== case drag
case MotionEvent.ACTION_MOVE:
handlerXYPos.post(runnableXYPos);
break;

此runnable执行15帧

runnableXYPos = new Runnable() {  

                @Override  
                public void run() 
                {  

                     Xpos.add(xP);
                     Ypos.add(yP);
                     MarkerSelected.add(MarkerName);
                     handlerXYPos.postDelayed(runnableXYPos, 66);  //convert 15 in milisec = 66


                }  
            };

当我按下按钮时,此代码有效,有时会添加正确数量的值,但是当我拖动手指时会增加额外的值。

2 个答案:

答案 0 :(得分:2)

当您拖动以下内容时

1) MotionEvent.ACTION_DOWN called once when you press to start drag

2) MotionEvent.ACTION_MOVE is called several times depending on your drag

3) MotionEvent.ACTION_UP: will get called once when you release to finish drag

因此无需在MotionEvent.ACTION_MOVE

中编写代码

答案 1 :(得分:2)

我不确定它是否是解决方案,但尝试不采取任何行动:

    case MotionEvent.ACTION_MOVE:    
         // handlerXYPos.post(runnableXYPos);      
        break;