简体   繁体   English

第一次恢复后Android onTouchListener Action_Up-MotionEvent延迟

[英]Android onTouchListener Action_Up-MotionEvent delayed after first resume

I am pretty new to Android and Java programming and was able to find a lot of solutions to my many questions here. 我对Android和Java编程很陌生,并且能够在这里找到很多解决方案。 However I find myself stuck now, without finding any solutions online. 但是我现在发现自己陷入困境,没有找到任何在线解决方案

I have written a simple app, which creates an onTouchListener in the onCreate-method of my MainActivity. 我编写了一个简单的应用程序,它在我的MainActivity的onCreate方法中创建了一个onTouchListener。 info() starts a new InfoActivity. info()启动一个新的InfoActivity。

If I kill my app and start it fresh, the first time I touch the info ImageView I see ACTION_DOWN and ACTION_UP in rapid succession and the new InfoActivity opens quickly. 如果我杀了我的应用程序并重新开始,我第一次触摸信息ImageView时,我会快速连续看到ACTION_DOWN和ACTION_UP,新的InfoActivity会快速打开。

However if I have resumed the MainActivity by either minimizing it or visiting the InfoActivity and returning (with finish()) now I notice a huge delay of about 2000ms between the ACTION_DOWN and ACTION_UP event. 但是,如果我通过最小化它或访问InfoActivity并返回(带有finish())来恢复MainActivity,我现在注意到ACTION_DOWN和ACTION_UP事件之间的大约2000ms的延迟。 The new activity is also delayed the same way, even if I move the info() call in the ACTION_DOWN case like in the code here. 新活动也以相同的方式延迟,即使我在ACTION_DOWN案例中移动info()调用,就像在此处的代码中一样。

As far as I can tell nothing happens during those two seconds of delay. 据我所知,在这两秒延迟期间没有任何事情发生。

Please respond with a method how I can troubleshoot this better. 请回答一种方法,我可以更好地解决这个问题。 I realize there is probably an easy way to find out, but I have spent literally hours searching and nothing I tried brought me closer to a solution. 我意识到可能有一个简单的方法可以找到,但我花了几个小时搜索,没有尝试过任何东西让我更接近解决方案。

Best regards 最好的祝福

Julius 朱利叶斯

    info = (ImageView) findViewById(R.id.info);
    info.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    Log.i("MOTION_EVENT","ACTION DOWN");
                    info.setImageResource(R.drawable.infoback);
                    info();
                    Log.i("MOTION_EVENT","ACTION INFO");
                    break;
                }
                case MotionEvent.ACTION_CANCEL:{
                    Log.i("MOTION_EVENT","ACTION CANCEL");
                    info.setImageResource(R.drawable.info);
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    Log.i("MOTION_EVENT","ACTION UP");
                    info.setImageResource(R.drawable.info);
                    break;
                }
            }
            return true;
        }
    });
    Log.i("TIMESTAMP","End onCreate");

Here's a log of starting the app, opening infoActivity, closing infoActivity and then trying to do so again, experiencing the slowdown. 这是启动应用程序的日志,打开infoActivity,关闭infoActivity,然后再次尝试这样做,体验减速。

06-29 02:12:12.529  15856-15856/de.juliusbier.shamebell I/TIMESTAMP﹕ Start onCreate
06-29 02:12:13.083  15856-15856/de.juliusbier.shamebell E/MediaPlayer﹕ Should have subtitle controller already set
06-29 02:12:13.083  15856-15856/de.juliusbier.shamebell I/TIMESTAMP﹕ End onCreate
06-29 02:12:13.084  15856-15856/de.juliusbier.shamebell I/TIMESTAMP﹕ Start onResume
06-29 02:12:13.084  15856-15856/de.juliusbier.shamebell I/SHAKER﹕ RESUME
06-29 02:12:13.084  15856-15856/de.juliusbier.shamebell I/TIMESTAMP﹕ End onResume
06-29 02:12:13.089  15856-15877/de.juliusbier.shamebell D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-29 02:12:13.092  15856-15856/de.juliusbier.shamebell D/Atlas﹕ Validating map...
06-29 02:12:13.099  15856-15856/de.juliusbier.shamebell E/MediaPlayer﹕ Should have subtitle controller already set
06-29 02:12:13.128  15856-15877/de.juliusbier.shamebell I/Adreno﹕ EGLInit: QTI Build: 01/29/15, 1bccc5d, I0ba6dce82d
06-29 02:12:13.138  15856-15877/de.juliusbier.shamebell I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-29 02:12:13.144  15856-15877/de.juliusbier.shamebell D/OpenGLRenderer﹕ Enabling debug mode 0
06-29 02:12:20.480  15856-15856/de.juliusbier.shamebell I/MOTION_EVENT﹕ ACTION DOWN
06-29 02:12:20.521  15856-15856/de.juliusbier.shamebell I/MOTION_EVENT﹕ ACTION INFO
06-29 02:12:20.557  15856-15856/de.juliusbier.shamebell I/MOTION_EVENT﹕ ACTION UP
06-29 02:12:20.563  15856-15856/de.juliusbier.shamebell I/INFO﹕ Start onCreate
06-29 02:12:20.578  15856-15856/de.juliusbier.shamebell I/INFO﹕ Finish onCreate
06-29 02:12:24.992  15856-15856/de.juliusbier.shamebell I/TIMESTAMP﹕ Start onResume
06-29 02:12:24.993  15856-15856/de.juliusbier.shamebell I/SHAKER﹕ RESUME
06-29 02:12:24.993  15856-15856/de.juliusbier.shamebell I/TIMESTAMP﹕ End onResume
06-29 02:13:03.367  15856-15856/de.juliusbier.shamebell I/MOTION_EVENT﹕ ACTION DOWN
06-29 02:13:03.375  15856-15856/de.juliusbier.shamebell I/MOTION_EVENT﹕ ACTION INFO
-----------------------------Here comes the delay-------------------------------------
06-29 02:13:05.540  15856-15856/de.juliusbier.shamebell I/MOTION_EVENT﹕ ACTION UP
06-29 02:13:05.542  15856-15856/de.juliusbier.shamebell I/INFO﹕ Start onCreate
06-29 02:13:05.551  15856-15856/de.juliusbier.shamebell I/INFO﹕ Finish onCreate

UPDATE: Taking out the setImageResource calls solves the issue, it seems to me like the drawing is somehow delayed and the new activity is not openened until all setImageResources have been dealt with. 更新:取出setImageResource调用解决了这个问题,在我看来,绘图以某种方式延迟,并且在处理完所有setImageResources之后,新活动才会打开。

        info = (ImageView) findViewById(R.id.info);
        info.setImageResource(R.drawable.info_selector);
        info.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                shaker.close();
                Intent infoIntent = new Intent(getBaseContext(), InfoActivity.class);
                startActivity(infoIntent);
            }
        });

With this code from Rajesh Batth, I get the same problems. 使用Rajesh Batth的代码,我遇到了同样的问题。 Notice that the code in the onClick(View v) is my former info() method. 请注意,onClick(View v)中的代码是我以前的info()方法。 shaker.close calls the following line in a shake detection class I adapted from the answer of this post : shaker.close在一个震动检测类中调用以下行我改编自这篇文章的答案:

        mgr.unregisterListener(listener);

Not closing the shaker or even taking out the shake detection completely does not change anything. 不关闭摇床或甚至完全取出摇动检测不会改变任何东西。

This has nothing to do with your onTouch specifically as it works normal. 这与你的onTouch没有任何关系,因为它正常工作。 The thing is when onResume() is called which means your Activity resumes super.onResume() is called, and the line actually does something, when you touch the Screen ondispatchtouch is called with calls your touch code line and does something-all this on a particular Thread,and both runs in a succession-hence the wait. 问题是当onResume()被调用时,这意味着你的Activity恢复了super.onResume()被调用,并且该行实际上做了一些事情,当你触摸屏幕ondispatchtouch被调用时调用你的触摸代码行并做了一些事情 - 这一切都在一个特定的线程,两者都在连续运行 - 因此等待。 In short - your onResume() does a lot of code and until its done calls to android on the main Thread a pretty frozen 简而言之 - 你的onResume()做了很多代码,直到它完成对主线程上的android的调用一个相当冻结

Edit-after-your-comment-&-edit 编辑后的注释 - & - 编辑

Sir, i still stand by the thread being busy, even though where i was approaching from was not the cause of the lag. 先生,我仍然坚持正在忙碌的线索,即使我接近的地方并不是滞后的原因。 If this happens when you come from onResume() well it is because of the android loading your required resources before carrying on, since setImageResource() actually runs on the UI thread, and also dependent on the size of the image that is why i think your problem was solved after commenting or taking out setImageResource() 如果你来自onResume()就好了这是因为android在继续之前加载了你需要的资源,因为setImageResource()实际上是在UI线程上运行的,而且还依赖于图像的大小,这就是为什么我认为注释或取出setImageResource()后问题解决了

If you are doing any time-consuming operation inside info() this could happen. 如果你在info()中进行任何耗时的操作,这可能会发生。 Also looking at the code it feels like the requirement can be achieved with onClickListener and selector drawable. 同时查看代码,感觉就像使用onClickListener和selector drawable可以实现这一要求。 A sample selector would be like this. 样本选择器就是这样的。 info_selector.xml info_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/infoback"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/infoback"/>
<item android:state_focused="true" android:drawable="@drawable/infoback"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/info"/>
</selector>

In Actitity 在Actitity

info = (ImageView) findViewById(R.id.info);
info.setImageResource(R.drawable.info_selector);
info.setOnClickListener(new OnClickListener(){
    public void onClick(View v){
       info();
    }
});

We had exactly the same behaviour, and in our case, the solution was simply to put 我们有完全相同的行为,在我们的例子中,解决方案只是放置

setFocusable(true);

into the constructor of your SurfaceView (which you probably have somewhere). 进入SurfaceView的构造函数(你可能在某处)。

In our case the SurfaceView looks like this: 在我们的例子中,SurfaceView看起来像这样:

public class GameSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

private GameActivity controller = null;
private InputComponent inputHandler = null;

public GameSurfaceView(GameActivity controller) {
    super(controller);
    this.controller = controller;
    getHolder().addCallback( this );
    setFocusable(true);
    Log.d("GameSurfaceView", "constructor.");
}
}

This little line with setFocusable helped for some weird reason. 这个带有setFocusable的小行有一些奇怪的原因。 However, I don't know, why the delay does not occur at the first start of the app. 但是,我不知道,为什么延迟不会在应用程序的第一次启动时发生。

Greez Greez

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

相关问题 使用OnTouchListener,MotionEvent.ACTION_UP时,应用程序意外停止 - App stops unexpectedly when using OnTouchListener, MotionEvent.ACTION_UP Android MotionEvent.ACTION_MOVE和MotionEvent.ACTION_UP - Android MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP scollview启动时未检测到android.view.MotionEvent.ACTION_UP - android.view.MotionEvent.ACTION_UP not detected when scollview kicks in 不调用MotionEvent.ACTION_UP - MotionEvent.ACTION_UP is not called OnTouchListener(MotionEvent)动画 - OnTouchListener (MotionEvent) animation 没有显示MotionEvent.ACTION_UP或MotionEvent.ACTION_CANCEL - not showing MotionEvent.ACTION_UP or MotionEvent.ACTION_CANCEL AndroidStudio MotionEvent:仅在第二个视图上检测到ACTION_UP,在第一个视图上启动ACTION_DOWN - AndroidStudio MotionEvent: detect only ACTION_UP on a second view, starting ACTION_DOWN on first view 在ACTION_DOWN中启动后,CountDownTimer在MotionEvent.ACTION_UP上未取消 - CountDownTimer not cancelled on MotionEvent.ACTION_UP after starting it in ACTION_DOWN 检查MotionEvent.ACTION_UP是否超出imageview - Check if MotionEvent.ACTION_UP is out of the imageview Android多点触控ACTION_POINTER_UP MotionEvent.getX IllegalArgumentException - Android Multi-Touch ACTION_POINTER_UP MotionEvent.getX IllegalArgumentException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM