Android java线程睡眠示例
我正在用Android做一个简单的动画应用程序。情况就是这样。我有一个按钮和4个文本视图。当用户单击按钮时,第一个文本视图应开始动画(闪烁效果),然后第二个文本视图,依此类推。这就是我迄今为止所尝试的:Android java线程睡眠示例,java,android,multithreading,Java,Android,Multithreading,我正在用Android做一个简单的动画应用程序。情况就是这样。我有一个按钮和4个文本视图。当用户单击按钮时,第一个文本视图应开始动画(闪烁效果),然后第二个文本视图,依此类推。这就是我迄今为止所尝试的: @Override public void onClick(View v) { // TODO Auto-generated method stub btGo.setEnabled(false); glowCircuit2(); glowCircuit3();
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btGo.setEnabled(false);
glowCircuit2();
glowCircuit3();
glowCircuit4();
}
private void glowCircuit2() {
new Thread(new Runnable()
{
@Override
public void run()
{
while (!Thread.interrupted())
try
{
Thread.sleep(3000);
runOnUiThread(new Runnable() // start actions in UI thread
{
@Override
public void run()
{
// this action have to be in UI thread
tvCircuit2.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.activecircuit, 0, 0);
tvCircuit2.startAnimation(animBlink);
}
});
}
catch (InterruptedException e)
{
// ooops
}
}
}).start(); // the while thread will start in BG thread
}
private void glowCircuit3() {
new Thread(new Runnable()
{
@Override
public void run()
{
while (!Thread.interrupted())
try
{
Thread.sleep(3000);
runOnUiThread(new Runnable() // start actions in UI thread
{
@Override
public void run()
{
// this action have to be in UI thread
tvCircuit3.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.activecircuit, 0, 0);
tvCircuit3.startAnimation(animBlink);
}
});
}
catch (InterruptedException e)
{
// ooops
}
}
}).start(); // the while thread will start in BG thread
}
private void glowCircuit4() {
new Thread(new Runnable()
{
@Override
public void run()
{
while (!Thread.interrupted())
try
{
Thread.sleep(3000);
runOnUiThread(new Runnable() // start actions in UI thread
{
@Override
public void run()
{
// this action have to be in UI thread
tvCircuit4.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.activecircuit, 0, 0);
tvCircuit4.startAnimation(animBlink);
}
});
}
catch (InterruptedException e)
{
// ooops
}
}
}).start(); // the while thread will start in BG thread
}
我得到了眨眼的效果,但我得到了同步的效果。我想要的是等待每个textview设置动画,而不是同时设置动画。你有什么聪明的主意来做这件事吗?非常感谢你的帮助。谢谢。您在这里使用线程,您永远不知道哪个线程首先获得优先级,哪个线程将开始。我建议您使用handler,创建不同的消息id将空消息发送给handler,Android执行以下操作:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btGo.setEnabled(false);
handler.sendEmptyMessage(1);
}
Handler handler = new Handler(){
onHandle(Messsage msg)
{
switch (msg.what)
{
case 1 : glowCircuit2();
handler.sendEmptyMessage(2);
break;
case 2: glowCircuit3();
handler.sendEmptyMessage(3);
break;
}
}
};
private void glowCircuit2(){
tvCircuit2.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.activecircuit, 0, 0);
tvCircuit2.startAnimation(animBlink);
}
更改代码
glowCircuit2();
glowCircuit3();
glowCircuit4();
对此
animation.setAnimationListener(new Animation.AnimationListener()
{
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
glowCircuit2();
}
});
animation1.setAnimationListener(new Animation.AnimationListener()
{
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
glowCircuit3();
}
});
animation2.setAnimationListener(new Animation.AnimationListener()
{
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
glowCircuit4();
}
});
像下面的代码一样,在第二个动画结束时开始第三个动画,在第三个动画结束时开始第四个动画。提示:要实现这一点,必须从
glowCircuit2()
中调用glowCircuit3()
,您还没有完成任何等待线程逐个执行动画的编码。有多种方法可以实现这一点,您应该阅读关于线程的how tojoin()
。通过使用Barriers
@sakura-nope,我已经用xml制作了闪烁动画。所以我现在要做的是等待textview显示,然后再显示下一个textview。@PC。我已经这样做了,但我仍然得到了相同的结果。那么我将把它放在哪里?那么我将把它放在一个方法中?我有点困惑抱歉,。