问题描述
欲正被播放的动画时锁定ontouch监听器。这是我的code。
I want to lock the ontouch listener when the animation is being played. this is my code.
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实施。我想,直到GIF动画完成一个循环temperory锁ontouch。请帮助。
I have tried to implement ontouch within ontouch but no use. I want to temperory lock ontouch till the gif animation completes a loop. Please help.
推荐答案
您可以尝试以下的如果的你知道GIF的运行时间:
You can try the following if you know the GIF's running time:
声明全局布尔变量:
boolean gifIsPlaying;
long PLAYING_TIME_OF_GIF = ???;
创建和添加GIFWebView到您的活动的看法后,设置 gifIsPlaying
到真正
。延迟后一个Runnable设置 gifIsPlaying
到假
在 PLAYING_TIME_OF_GIF
:
After creating and adding GIFWebView to your activity's view, set gifIsPlaying
to true
. Delayed-post a Runnable to set gifIsPlaying
to false
after PLAYING_TIME_OF_GIF
:
gifIsPlaying = true;
new Handler().postDelayed(new Runnable() {
public void run() {
gifIsPlaying = false;
}
}, PLAYING_TIME_OF_GIF);
PLAYING_TIME_OF_GIF
将是长
变量。
在您的onTouch(视图,MotionEvent):
Inside your 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;
}
如果这个方法对你的作品,考虑创建一个处理程序
一旦和重用它。这同样适用于的Runnable
。
If this approach works for you, consider creating a Handler
once and reusing it. Same goes for the Runnable
.
我不认为有任何其他的方式来解决这个问题。当然有没有回调方法来通知你,GIF已经走完。
I don't think there's any other way to solve this problem. There certainly isn't a callback method to inform you that the GIF has run its course.
这篇关于锁定ontouch监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!