简体   繁体   English

Android surfaceview onTouchEvent限制?

[英]Android surfaceview onTouchEvent limiting?

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. 当您有用于动画/游戏/ ...的surfaceView类时,您在同一类中具有onTouchEvent()方法和绘图/屏幕刷新方法。 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). 现在,我只想休眠onTouchEvent方法,这样就不会每秒调用+ -80次并淹没CPU(并因此导致延迟)。 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. ** Edit88我正试图阻止onTouchEvent()调用过于频繁。 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. 首先是我的SurfaceView类的一段代码。 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. 请注意,我的第一个Log.d如何返回某些内容,而第二个却没有。 This tells me that CurrentlySleeping is never false ? 这告诉我,CurrentlySleeping永远不会虚假? I still have not figured things out... ? 我仍然没有弄清楚事情...?

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) 我认为,一开始不监听onTouch事件会更简单,而不是在确实发生onTouch事件时阻止处理代码,在这种情况下,您仍然会有调用该函数的开销(在这种情况下,当你不想的时候

So implement the onTouchListener interface and set it for the view using setOnTouchListener . 因此,实现onTouchListener接口,并使用setOnTouchListener将其设置为视图。 Now when you don't want to listen to any onTouch events for a view set it to NULL. 现在,当您不想监听任何onTouch事件时,将其设置为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. 现在,不要使用睡眠,而是尝试使用“警报”或将延迟的消息发布到线程处理程序中,以在稍后的时间重置视图的onTouchListener (Personally, I think it is much neater than sleep :) ) (个人而言,我认为它比睡觉要整洁得多:))

If the class has an onTouchEvent method you cannot prevent it from being called. 如果该类具有onTouchEvent方法,则无法阻止其被调用。 Just have onTouchEvent return false and do nothing else. 只是让onTouchEvent返回false而不做其他任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM