锁定ontouch监听器

时间:2013-08-17 09:46:25

标签: android ontouchevent

我想在播放动画时锁定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,但没有用。我希望temperory锁定ontouch,直到gif动​​画完成循环。请帮忙。

1 个答案:

答案 0 :(得分:1)

您可以尝试以下,如果您知道GIF的运行时间:

声明一个全局布尔变量:

boolean gifIsPlaying;
long PLAYING_TIME_OF_GIF = ???;

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

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已经运行了。