检查视图是否被按下的好方法

时间:2013-01-14 01:31:12

标签: android optimization multi-touch

我正在尝试管理可与许多按钮配合使用的多任务应用。

为此我做了这个功能:

public boolean isTouched(View view, MotionEvent event) {
    boolean touched = false;
    int count = event.getPointerCount();

    int[] location = { 0, 0 };
    view.getLocationOnScreen(location);
    Point min = new Point(location[0], location[1]);
    Point max = new Point(min.x + view.getWidth(), min.y + view.getHeight());

    for (int i = 0; i < count; i++) {


        int rawX = (int) event.getX(i);
        int rawY = (int) event.getY(i);
        if(rawX>=min.x && rawX<=max.x && rawY>=min.y && rawY<=max.y){
            //Log.d("mylog", "***Found a view: " + v.getId());
            touched = true;
            if (event.getAction() == MotionEvent.ACTION_UP) touched = false;
        }
    }
    return touched;
    }

拦截onTouchEvent并管理两个ImageView

的活动的函数覆盖
@Override
public boolean onTouchEvent (MotionEvent event) {

    if (isTouched(button1,event)) {
        // it is pressed
        if ((Integer)button1.getTag() == 0) {  // but before wasn't pressed
            mp1.start();
            button1.setBackgroundResource(R.drawable.happy_on);
            button1.setTag(1);

            // start music 1
        }
    } else {
        // it isn't pressed
        if ((Integer)button1.getTag() == 1) {   // and before it was pressed
            mp1.stop();
            try {
                mp1.prepare();
            } catch (IOException e) {
                e.printStackTrace(); 
            }  catch (IllegalStateException e) {
                Log.e("hb","illegal state exception mp2") ;
                mp1= MediaPlayer.create(getBaseContext(), R.raw.female01) ;

            }

            button1.setBackgroundResource(R.drawable.happy_off);
            button1.setTag(0);
            // stop music 1
        }
    }

         if (isTouched(button2,event)) {
             // it is pressed
             if ((Integer)button2.getTag() == 0) {  // but before wasn't pressed
                 mp2.start();

                 button2.setBackgroundResource(R.drawable.to_on);
                 button2.setTag(1);
                 // start music 2
             }

         } else {
             // it isn't pressed
             if ((Integer)button2.getTag() == 1) {   // and before it was pressed
                 mp2.stop();
                 try {
                     mp2.prepare();
                 } catch (IOException e) {
                     e.printStackTrace(); 
                 } catch (IllegalStateException e) {
                     Log.e("hb","illegal state exception mp2") ;
                     mp2= MediaPlayer.create(getBaseContext(), R.raw.female02) ;                     }

                 button2.setBackgroundResource(R.drawable.to_off);
                 button2.setTag(0);
                 // stop music 1
             }
         }

    return false;

    }

按钮(我使用的是imageview)像钢琴键盘一样被按下:你可以用一根,两根手指将它传递过来,当指针进入视图时,异步动作开始。在event_up或指针离开按钮时,动作停止。

它有效,但它是一个非常慢的方法:它被许多action_move事件所淹没。

有谁知道一种优化这种多点触控管理方式的方法?

编辑:mp1和mp2是mp3媒体播放器对象。当我从一个按钮传递到另一个按钮而没有释放指针时它会工作,但是有一个毫秒差距使旋律跳跃。我想做的效果就像钢琴的键盘,没有间隙。

我认为差距是针对函数isTouched的权重产生的。我怎样才能减少这个重量?

谢谢

1 个答案:

答案 0 :(得分:0)

您应该使用具有多个状态drawable而非Button的常规ImageView,然后在XML中使用setOnClickListener(...)或定义android:onclick。我认为你目前面临的问题只是你做出的一个有缺陷的设计决定的第一个方面。