【问题标题】:Lock ontouch listener锁定 ontouch 监听器
【发布时间】:2013-08-17 09:46:25
【问题描述】:

我想在动画播放时锁定 ontouch 监听器。这是我的代码。

public class MainActivity extends Activity implements OnTouchListener {


 boolean gifIsPlaying;
 long PLAYING_TIME_OF_GIF = 111;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    GIFWebView view = new GIFWebView
            (this, "file:///android_asset/imageedit_ball.gif");


    gifIsPlaying = true;

    new Handler().postDelayed(new Runnable() {
        public void run() {
            gifIsPlaying = false;
        }
    }, PLAYING_TIME_OF_GIF);


    setContentView(view);
    view.setOnTouchListener(this);
    }       

public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

    if (gifIsPlaying) {
        // No response to touch events
    } else {
        // Respond to touch events
        GIFWebView view1 = new GIFWebView
                (this, "file:///android_asset/imageedit_ball.gif");

        gifIsPlaying = true;

        new Handler().postDelayed(new Runnable() {
            public void run() {
                gifIsPlaying = false;
            }
        }, PLAYING_TIME_OF_GIF);

        setContentView(view1);
    }

    // Consume touch event
    return true;
}

}

我尝试在 ontouch 中实现 ontouch,但没有用。我想临时锁定 ontouch 直到 gif 动画完成一个循环。请帮忙。

【问题讨论】:

  • 在onTouch里面使用一个布尔变量来执行代码……你可以使用一个TimerTask等待一段时间,然后切换布尔变量。
  • 公共布尔 isAnimationOn = false; public boolean onTouch(View v, MotionEvent event){ if (isAnimationOn) return false;否则返回真;我试过用这个但不工作
  • 提供你使用过的正确代码实现..
  • @Override public boolean onTouch(View v, MotionEvent event) { // TODO 自动生成的方法存根 if (event.getAction() == MotionEvent.ACTION_DOWN) { if (loaded) { return false ; } else { GIFWebView view1 = new GIFWebView (this, "file:///android_asset/imageedit_ball.gif");设置内容视图(视图 1); view1.setOnTouchListener(this); } } 返回真; }

标签: android touch-event


【解决方案1】:

你可以试试下面的如果你知道GIF的运行时间:

声明一个全局布尔变量:

boolean gifIsPlaying;
long PLAYING_TIME_OF_GIF = ???;

创建 GIFWebView 并将其添加到活动视图后,将 gifIsPlaying 设置为 true。延迟发布一个 Runnable 以在 PLAYING_TIME_OF_GIF 之后将 gifIsPlaying 设置为 false

gifIsPlaying = true;

new Handler().postDelayed(new Runnable() {
    public void run() {
        gifIsPlaying = false;
    }
}, PLAYING_TIME_OF_GIF);

PLAYING_TIME_OF_GIF 将是一个 long 变量。

在你的 onTouch(View, MotionEvent) 中:

public boolean onTouch(View v, MotionEvent event) {
    if (gifIsPlaying) {
        // No response to touch events
    } else {
        // Respond to touch events
        GIFWebView view1 = new GIFWebView
                (this, "file:///android_asset/imageedit_ball.gif");

        gifIsPlaying = true;

        new Handler().postDelayed(new Runnable() {
            public void run() {
                gifIsPlaying = false;
            }
        }, PLAYING_TIME_OF_GIF);

        setContentView(view1);
    }

    // Consume touch event
    return true;
}

如果此方法适合您,请考虑创建一次 Handler 并重复使用它。 Runnable 也是如此。

我认为没有其他方法可以解决这个问题。肯定没有回调方法来通知您 GIF 已运行。

【讨论】:

  • 它不起作用,实际上一开始触摸事件正在发生,但后来触摸事件不起作用。
  • @Meghs 你能在上面的问题中发布你的整个活动吗?
  • @Meghs Handler.postDelayed(Runnable, long)long 参数是以毫秒为单位的延迟时间。因此,111 将转换为 0.111 seconds。如果您的 GIF 的播放时间为 111 秒,则将 PLAYING_TIME_OF_GIF 定义为等于 111000
  • 如何计算动画的准确 gif 时间?因为我不知道确切的时间,我想赶上时间,以便我可以给 PLAYING_TIME_OF_GIF 提供确切的时间
  • @Meghs 一般情况下,GIF 会循环播放以无休止地播放。所以,当你说I want to lock the ontouch listener when the animation is being played 时,它有点奇怪。因为,GIF 会无休止地播放。任何状况之下。如果这只是您需要播放的一个 GIF,请使用秒表并自己计时。使用持续时间设置PLAYING_TIME_OF_GIF。除此之外,我不确定你能做什么。
猜你喜欢
  • 1970-01-01
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多