如何继续更新屏幕而不淹没 onTouchEvent ? (安卓表面视图)
当您有一个用于动画/游戏/...的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这个:
另外看看
MotionEvent
javadoc 对不同事件类型的解释。通过重写
onTouchEvent
,您可以控制该方法中发生的情况。您可以在 Activity 中设置一个标志 (boolean touchEnabled = true
) 并在onTouchEvent()
中进行检查:更新
如果您的问题只是简单
onTouchEvent()
触发得太快,那么你可以这样做:Have a look at this:
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 inonTouchEvent()
:UPDATE
If your issue is simply that the
onTouchEvent()
is firing too fast, then you could do something like this: