Android SurfaceView onTouchEvent 限制?

发布于 2024-11-15 13:36:37 字数 1057 浏览 3 评论 0原文

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

**编辑88 我试图阻止 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.

**Edit88
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技术交流群

发布评论

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

评论(2

计㈡愣 2024-11-22 13:36:37

我认为首先不监听 onTouch 事件而不是在 onTouch 事件确实发生时阻塞处理代码会更简单,在这种情况下,您仍然会有调用函数的开销(在这种情况下)当你不需要它时)

因此,实现onTouchListener接口并使用setOnTouchListener为视图设置它。现在,当您不想监听视图的任何 onTouch 事件时,请将其设置为 NULL。

现在,不要使用睡眠,而是尝试使用 Alarm 或向线程处理程序发布延迟消息,以便稍后重置视图的 onTouchListener 。 (我个人认为这比睡眠要整洁得多:))

I think it would be simpler not to listen to the onTouch events in the first place instead of blocking out the processing code when the onTouch event does occur, in this case you will still have an overhead of the function getting called (and in this case when you don't want it)

So implement the onTouchListener interface and set it for the view using setOnTouchListener. Now when you don't want to listen to any onTouch events for a view set it to NULL.

Now instead of using sleep, try using an Alarm or post a delayed message to your thread handler to reset the onTouchListener for the view at a later point of time. (Personally, I think it is much neater than sleep :) )

邮友 2024-11-22 13:36:37

如果该类具有 onTouchEvent 方法,您无法阻止它被调用。只需让 onTouchEvent 返回 false 即可,不执行任何其他操作。

If the class has an onTouchEvent method you cannot prevent it from being called. Just have onTouchEvent return false and do nothing else.

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