Search code examples
androidtouchscreenmulti-touch

Android: Increase responsiveness of phone screen?


Is there a way to increase responsiveness of a device for a particular application? Specifically, right now my touch events are definitely triggering, but they are delayed by a fraction of a second. Since my application is a music/rhythm app, any delay is very bad. I was just wondering, if there was anything I can do in the Manifest that would increase responsiveness. Or perhaps some suggestions for making the code work faster? I have to create multiple threads in the app as well to handle multitouch, perhaps there is an efficient way of doing that?

Best, Aneem

EDIT:

public boolean onTouchEvent(final MotionEvent ev) {
    if(ev.getAction()==ev.ACTION_DOWN||ev.getActionMasked()==ev.ACTION_POINTER_DOWN){                   
        AudioManager mgr = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
        int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        PointF[] touchPoints = new PointF[ev.getPointerCount()];
        for(int i = 0; i < ev.getPointerCount(); i++){
            touchPoints[i] = new PointF(ev.getX(i),ev.getY(i));
        }
        for(final PointF point : touchPoints){
            x = ev.getX(ev.getActionIndex());
            y = ev.getY(ev.getActionIndex());
            int ptx = (int) (x - x%widthIncrement);
            int pty = (int) (y - y%heightIncrement);
            playSound(pointMap.get(new Point(ptx,pty)));
        }
        return true;
    }
    return true;
}

public void playSound(int sound){
    AudioManager mgr = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
    int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
    database.play(sound, streamVolume, streamVolume, 1, 0, 1);
}

Solution

  • If your UI is sluggish, you are doing too much work in response to UI events. If you have to do anything non-trivial (load a sound file, etc.) in response to a UI event, then you should package that work into an AsyncTask or other kind of worker thread. The UI response method should then just fire up the thread to do the heavy work, update the UI, and return.

    The article Painless Threading has more details about how to do all this.