如何继续更新屏幕而不淹没 onTouchEvent ? (安卓表面视图)

发布于 2024-11-15 11:09:48 字数 1161 浏览 2 评论 0原文

当您有一个用于动画/游戏/...的 SurfaceView 类时,您在同一类中就有 onTouchEvent() 方法和绘图/屏幕刷新方法。现在我只想休眠 onTouchEvent 方法,这样它就不会每秒被调用 +-80 次并淹没 cpu(从而导致延迟)。问题是,如果我在这堂课上睡觉,它也会停止绘图/更新/刷新。对此有什么好的解决方案吗?

编辑
@tanner,这确实是我正在尝试的,阻止 onTouchEvent() 被过于频繁地调用。我的尝试:

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d("firstTest",Integer.toString(event.getAction()));   //shows up
    if (!sleeper.CurrentlySleeping) {
        Log.d("2ndTest",Integer.toString(event.getAction())); //not showing up
        ~~  some calculations and stuff  ~~

public class TouchSleeper extends Thread {
// If Thread is sleeping or not.
public static Boolean CurrentlySleeping;
public TouchSleeper() {
    CurrentlySleeping = false;
    this.setPriority(MIN_PRIORITY);
    }

@Override
public void run() {
    try {
        while (true) {
            CurrentlySleeping = true;
            TouchSleeper.sleep(50);
            CurrentlySleeping = false; }
        ~end~

首先是我的 SurfaceView 类中的一段代码。类/线程的第二部分,它不断改变布尔值。请注意我的第一个 Log.d 如何返回某些内容,而第二个 Log.d 则没有返回。这告诉我 CurrentSleeping 永远不会是假的?我还没弄清楚事情...?

When you have a surfaceView class, used for animation/game/... you have the onTouchEvent() method and the drawing/screen-refreshing method in that same class. Now I want to just sleep the onTouchEvent method, so it doesn't get called +-80 times a second and flooding the cpu (and therefor causing lag). The problem is that if I sleep this class, it also stops drawing/updating/refreshing. What could be a good solution for this?

Edit
@tanner, that's indeed what i'm trying, to stop onTouchEvent() to be called too frequently. my attempt:

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d("firstTest",Integer.toString(event.getAction()));   //shows up
    if (!sleeper.CurrentlySleeping) {
        Log.d("2ndTest",Integer.toString(event.getAction())); //not showing up
        ~~  some calculations and stuff  ~~

.

public class TouchSleeper extends Thread {
// If Thread is sleeping or not.
public static Boolean CurrentlySleeping;
public TouchSleeper() {
    CurrentlySleeping = false;
    this.setPriority(MIN_PRIORITY);
    }

@Override
public void run() {
    try {
        while (true) {
            CurrentlySleeping = true;
            TouchSleeper.sleep(50);
            CurrentlySleeping = false; }
        ~end~

First a piece of code from my SurfaceView class. Second piece of a class/thread, wich keeps changing a boolean value. Note how my first Log.d does return something, and the second one doesn't. This tells me that CurrentlySleeping is never false ? I still have not figured things out... ?

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

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

发布评论

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

评论(1

柠檬色的秋千 2024-11-22 11:09:48

看看这个:

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();

    if (action == MotionEvent.ACTION_UP) {
        // Do something here
    }
}

另外看看 MotionEvent javadoc 对不同事件类型的解释。

通过重写onTouchEvent,您可以控制该方法中发生的情况。您可以在 Activity 中设置一个标志 (boolean touchEnabled = true) 并在 onTouchEvent() 中进行检查:

if (!touchEnabled) {
    return;
}

// continue with whatever you wanted to do.

// Remove the following line if you don't want to pass the event to the parent.
super.onTouchEvent(event);

更新

如果您的问题只是简单onTouchEvent() 触发得太快,那么你可以这样做:

private long lastTime = -1;

private long threshold = 1000;

@Override
public boolean onTouchEvent(MotionEvent event) {
    long now = System.currentTimeMillis();
    if (lastTime > -1 && (now - lastTime) < threshold) {
        // Return if a touch event was receive less than "threshold" time ago
        return;
    }

    lastTime = now;

    super.onTouchEvent(event);
}

Have a look at this:

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();

    if (action == MotionEvent.ACTION_UP) {
        // Do something here
    }
}

Also have a look at the MotionEvent javadoc for an explanation of the different event types.

By overriding onTouchEvent, you can control what happens in that method. You could set a flag in your Activity (boolean touchEnabled = true) and do a check in onTouchEvent():

if (!touchEnabled) {
    return;
}

// continue with whatever you wanted to do.

// Remove the following line if you don't want to pass the event to the parent.
super.onTouchEvent(event);

UPDATE

If your issue is simply that the onTouchEvent() is firing too fast, then you could do something like this:

private long lastTime = -1;

private long threshold = 1000;

@Override
public boolean onTouchEvent(MotionEvent event) {
    long now = System.currentTimeMillis();
    if (lastTime > -1 && (now - lastTime) < threshold) {
        // Return if a touch event was receive less than "threshold" time ago
        return;
    }

    lastTime = now;

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