如何获得连续的触摸事件?

时间:2010-03-29 16:21:20

标签: android events touch continuous

我的课程扩展了View,我需要在其上获得连续的触摸事件。

如果我使用:

public boolean onTouchEvent(MotionEvent me) {

    if(me.getAction()==MotionEvent.ACTION_DOWN) {
        myAction();
    }
    return true;
}

...触摸事件被捕获一次。

如果我需要连续触摸而不移动手指怎么办? 请告诉我,我不需要使用线程或计时器。我的应用程序已经太沉重了。

感谢。

6 个答案:

答案 0 :(得分:15)

使用if(me.getAction() == MotionEvent.ACTION_MOVE)。将手指100%完全保留在屏幕上是不可能的,因此每次手指移动时都会调用Action_Move,即使它只是一两个像素。

你也可以听me.getAction() == MotionEvent.ACTION_UP - 直到发生这种情况,用户仍然必须将手指放在屏幕上。

答案 1 :(得分:5)

您需要为元素设置此属性         机器人:可聚焦=“真”         机器人:可点击=“真” 如果没有,只需产生下行动作。

答案 2 :(得分:4)

她是一个简单的代码片段,展示了如何处理持续触摸事件。当您触摸设备并按住触摸并移动取景器时,会执行触摸移动操作。

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    if(isTsunami){
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Write your code to perform an action on down
            break;
        case MotionEvent.ACTION_MOVE:
            // Write your code to perform an action on contineus touch move
            break;
        case MotionEvent.ACTION_UP:
            // Write your code to perform an action on touch up
            break;
    }
    }
    return true;
}

答案 3 :(得分:1)

试试这个。它对我有用:

public static OnTouchListener loadContainerOnTouchListener() {
    OnTouchListener listener = new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        LinearLayout layout = (LinearLayout)v;
        for(int i =0; i< layout.getChildCount(); i++)
        {
            View view = layout.getChildAt(i);
            Rect outRect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
            if(outRect.contains((int)event.getX(), (int)event.getY()))
            {
                Log.d(this.getClass().getName(), String.format("Over view.id[%d]", view.getId()));
            }
        }

    }

请记住:您设置的监听器必须是容器布局(网格,相对,线性)。

LinearLayout layout = findViewById(R.id.yourlayoutid);
layout.setOnTouchListener(HelperClass.loadContainerOnTouchListener());

答案 4 :(得分:1)

这可能会有所帮助,

requestDisallowInterceptTouchEvent(true);
父视图上的

,像这样 -

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            view.getParent().requestDisallowInterceptTouchEvent(true);
            switch(motionEvent.getAction()){
            }

            return false; 

         }

答案 5 :(得分:0)

我正在使用用作拇指控制的自定义视图制作游戏。 。 。这就是我做的事情

float x = 0, y = 0;

@Override
public boolean onTouchEvent(MotionEvent event) {
    x = event.getX();
    y = event.getY();

    // handle touch events with 
    switch( event.getActionMasked() ) {
        case MotionEvent.ACTION_DOWN :
            if(cont)
            {
                // remove any previous callbacks
                removeCallbacks(contin);

                // post new runnable
                postDelayed(contin, 10);
            }
            invalidate();
            return true;
        case MotionEvent.ACTION_MOVE :
            if(!cont && thumbing != null)
            {
                //  do non-continuous operations here
            }
            invalidate();       
            return true;
        case MotionEvent.ACTION_UP :

            // set runnable condition to false
            x = 0;

            // remove the callbacks to the thread
            removeCallbacks(contin);
            invalidate();
            return true;
        default :
            return super.onTouchEvent(event);
    }
}

public boolean cont = false;

// sets input to continuous
public void set_continuous(boolean b) { cont = b; }


public Runnable contin = new Runnable()
{

    @Override
    public void run() {
        if(x != 0)
        {
            //  do continuous operations here
            postDelayed(this, 10);
        }
    }

};

但请注意,请确保在您的主要活动中调用此视图会通过onPause方法手动删除回调,如下所示

@Override
protected void onPause() {
    if(left.cont) left.removeCallbacks(left.contin);
    if(right.cont) right.removeCallbacks(left.contin); 
    super.onPause();
}

这样一来,如果你暂停并回来,触摸事件就不会被处理两次,而且视图也没有线程的开销。

**在三星Galaxy S3上测试**硬件加速**

相关问题