Android 具有延迟的操作序列
我正试图让我的应用程序显示一系列图像,一个接一个地显示。目前我的java看起来像这样:Android 具有延迟的操作序列,android,Android,我正试图让我的应用程序显示一系列图像,一个接一个地显示。目前我的java看起来像这样: arrow1.setVisibility(View.VISIBLE); Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { arrow1.setVisibility(View.INVISIBLE);
arrow1.setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
arrow1.setVisibility(View.INVISIBLE);
arrow2.setVisibility(View.VISIBLE);
}
}, 1000);
handler.postDelayed(new Runnable() {
public void run() {
arrow2.setVisibility(View.INVISIBLE);
arrow3.setVisibility(View.VISIBLE);
}
}, 1000);
我没有收到任何错误,但它也没有像我预期的那样工作。箭头2根本不显示,应用程序直接从箭头1移动到箭头3,稍微有点延迟。我的第二个handler.postDelayed(新的Runnable()函数)被重写了吗?在这种情况下,我应该如何进行延迟呢?您可以这样尝试
private static final int TotalLoopCount = 2;
private int count = 0;
private int mCurrentLoopCount = 0;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Your code
}
@Override
protected void onResume() {
super.onResume();
handler.postDelayed(runnable, 0);
}
@Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(runnable);
}
Runnable runnable = new Runnable() {
@Override
public void run() {
arrow1.setVisibility(View.INVISIBLE);
arrow2.setVisibility(View.INVISIBLE);
arrow3.setVisibility(View.INVISIBLE);
if(count == 0) {
arrow1.setVisibility(View.VISIBLE);
} else if(count == 1) {
arrow2.setVisibility(View.VISIBLE);
} else {
arrow3.setVisibility(View.VISIBLE);
}
count++;
if(count == 3) {
count = 0;
mCurrentLoopCount++;
}
if(mCurrentLoopCount < TotalLoopCount) {
handler.postDelayed(runnable, 3000);
}
}
};
private static final int totaloopcount=2;
私有整数计数=0;
private int mCurrentLoopCount=0;
Handler=newhandler();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//你的代码
}
@凌驾
受保护的void onResume(){
super.onResume();
handler.postDelayed(可运行,0);
}
@凌驾
受保护的void onPause(){
super.onPause();
handler.removeCallbacks(可运行);
}
Runnable Runnable=新的Runnable(){
@凌驾
公开募捐{
箭头1.设置可见性(视图.不可见);
箭头2.设置可见性(视图.不可见);
箭头3.设置可见性(视图.不可见);
如果(计数=0){
箭头1.设置可见性(视图.可见);
}否则如果(计数=1){
箭头2.设置可见性(视图.可见);
}否则{
箭头3.设置可见性(视图.可见);
}
计数++;
如果(计数=3){
计数=0;
mCurrentLoopCount++;
}
if(mCurrentLoopCount
您也可以按如下方式使用倒计时。有关详细信息,请参阅
对于3个图像,将millisInFuture
设置为countDownInterval*3,对于图像之间的延迟,将countDownInterval
设置为
long countDownInterval = 1000; // 1sec interval
long millisInFuture = countDownInterval*10; // 10sec total time
new CountDownTimer(millisInFuture, countDownInterval) {
public void onTick(long millisUntilFinished) {
arrow1.setVisibility(millisUntilFinished < millisInFuture ? View.VISIBLE:View.INVISIBLE);
arrow2.setVisibility(millisUntilFinished > 0 ? View.VISIBLE:View.INVISIBLE);
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
长倒计时间隔=1000;//1秒间隔
long millisInFuture=倒计时间隔*10;//10秒总时间
新的倒计时(毫秒未来,倒计时间隔){
公共void onTick(长毫秒未完成){
箭头1.设置可见性(MillsUntilFinished0?视图。可见:视图。不可见);
mTextField.setText(“剩余秒数:+millisuntiltfinished/1000”);
}
公共无效onFinish(){
mTextField.setText(“完成!”);
}
}.start();
我无法从内部类访问变量处理程序而不声明它现在正在运行!我如何让它在完成几次之后停止?几次的意思是..?在它在所有图像中循环两次之后,让它停止运行即可arrow3@Roonil,更新了我的答案。