0

我想在播放动画时锁定 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 动画完成一个循环。请帮忙。

4

1 回答 1

1

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

声明一个全局布尔变量:

boolean gifIsPlaying;
long PLAYING_TIME_OF_GIF = ???;

创建 GIFWebView 并将其添加到活动视图后,设置gifIsPlayingtrue. 延迟发布 Runnable 以设置gifIsPlayingfalseafter 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 已运行。

于 2013-08-21T08:32:35.757 回答