我想在播放动画时锁定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动画完成循环。请帮忙。
答案 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已经运行了。