Android:提高手机屏幕的响应速度?

发布于 2024-12-25 01:11:50 字数 1381 浏览 1 评论 0原文

有没有办法提高设备对特定应用程序的响应能力?具体来说,现在我的触摸事件肯定会触发,但它们会延迟几分之一秒。由于我的应用程序是音乐/节奏应用程序,因此任何延迟都非常糟糕。我只是想知道,我是否可以在清单中做任何事情来提高响应能力。或者也许有一些让代码运行得更快的建议?我还必须在应用程序中创建多个线程来处理多点触控,也许有一种有效的方法可以做到这一点?

最好的, 阿尼姆

编辑:

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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2025-01-01 01:11:50

如果您的 UI 反应迟缓,则说明您在响应 UI 事件方面做了太多工作。如果您必须执行任何重要操作(加载声音文件等)来响应 UI 事件,那么您应该将该工作打包到 AsyncTask 或其他类型的工作线程。然后,UI 响应方法应该启动线程来完成繁重的工作,更新 UI,然后返回。

文章Painless Threading 提供了有关如何执行所有这些操作的更多详细信息。

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.

就像说晚安 2025-01-01 01:11:50

您应该在接收触摸事件的 UI 线程上处理这些事件。您不需要使用多个线程来处理多个触摸点。有关所有活动触摸点的数据将包含在您收到的单个 MotionEvent 对象中。

如果您遇到一些计时问题,您可能会发现 MotionEvent 提供的事件时间值很有用。您可以获得由 MotionEvent 表示的最近事件以及已批处理到其中的任何历史事件的事件发生时间。您可以在处理事件时将事件时间与当前时间进行比较,从而允许您检查“时间倒流”以正确确定事件是否按照您正在寻找的节奏发生。 (这里使用的当前时间可以从 获取SystemClock.uptimeMillis().)

相关方法:
MotionEvent#getEventTime()
MotionEvent#getHistoricalEventTime(int)

作为 Ted Hopp请注意,您还应该尝试将任何重量级处理移出 UI 线程。响应 UI 事件的速度越快,队列中待处理的触摸事件的速度就越快。

You should handle touch events on your UI thread where they are received. You do not need to use multiple threads to handle multiple touch points. Data about all active touch points will be contained in the single MotionEvent object you receive.

If you're having some timing issues you might find the event time values provided by MotionEvent useful. You can get the time of when the event occurred both for the most recent event expressed by the MotionEvent as well as any historical events that have been batched into it. You can compare the event time with the current time when you process the event, allowing you to check "back in time" to correctly determine if the event occurred on the rhythm you're looking for. (The current time in the scale used here can be obtained from SystemClock.uptimeMillis().)

Relevant methods:
MotionEvent#getEventTime()
MotionEvent#getHistoricalEventTime(int)

As Ted Hopp notes, you should also try to move any heavyweight processing out of your UI thread. The faster you respond to UI events, the faster you will get to pending touch events in the queue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文