【问题标题】:Move finger across buttons in android apps在 Android 应用程序中跨按钮移动手指
【发布时间】:2013-10-27 21:13:29
【问题描述】:

当用户触摸按钮时应播放声音。有很多按钮,每个按钮都有不同的声音。当用户在所有按钮上移动手指时,应播放所有声音。像钢琴应用程序之类的东西。我该怎么做?我尝试使用 ontouch lister,但它不起作用。

【问题讨论】:

    标签: android touch touch-event ontouchlistener


    【解决方案1】:

    在您的 onTouch 方法中,您应该捕获不同的事件,从用户 TouchDown 开始,然后是 TouchMove,最后是 TouchUp。查找所有 x,y 坐标是否在您的按钮区域中,如果是,则播放声音。

    确保当前按钮选择与前一个不同,否则,如果您的手指在同一个按钮上移动,则会触发同一个按钮的另一个事件,并且您在移动手指时会听到大量声音:

    伪代码:

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
    
        switch(event.getAction())
        {
    
        case MotionEvent.ACTION_UP:     // stop here
            break;
        case MotionEvent.ACTION_DOWN:   // start here
            break;
        case MotionEvent.ACTION_MOVE:   
                // See if new x y co-ordinates in your buttonRect area 
                // RectF [] buttonRect = new buttonRect[10] ;
                for(int i = 0; i < 10; i++)
                {
                    if( buttonRect[i].contains(event.getX(), event.getY()))
                    {
                        // if it's a new button found than previously touch, play a sound
    
                        // store the button number that's been tapped by user
    
                    }
                }
    
    
            break;
        }
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 2012-11-06
      相关资源
      最近更新 更多