我有一个活动游戏.用户将点击图片:
用户有5秒钟的时间点击并选择正确的一个. 5秒后,正确的图像视图将以不同方式显示以引起注意,并在5秒后再次显示,用户将看到下一级别.
用户无需等待5秒即可点击,因此点击时应立即进入下一级别.
我正在动态创建视图和图像视图.图像视图从本地数据库sqlite作为字节进行组合.
我的方法是在oncreate方法中创建.我使用for或while循环.每次迭代都是一个游戏级别.每个级别都有不同的图像视图和不同的图像视图数.
为了延迟5 5秒,我不得不使用处理程序,线程,倒数计时器.每一个都引起了问题.
在使用处理程序时,当用户在5秒之前点击时,我无法终止当前会话.
我不知道这是不是正确的方法.
您可以在下面看到我的代码:
长期解释和代码
计时器:
nCreate() { for (int i = 0; i < 2; i++) { handler.postDelayed(new Runnable() { public voID run() { imageAnswer.setimageBitmap(bmp); imageAnswer.setID(R.ID.imgAnswer); //adding image to screen finalRLayout.removeAllVIEws(); finalRLayout.addVIEw(imageAnswer,rLayParams); //starting timer,if no answer in 5 seconds,run again timer startTimer(); //clickListerner imageAnswer.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { //if clicks true,cancel timer and go to next iteration of for loop if (vIEw == findVIEwByID(R.ID.imgAnswer)) { Log.d(TAG,"clicked"); //PUT IN CODE HERE TO GET NEXT IMAGE cancelTimer(); } else { //if not true,run again timer for last chance Log.d(TAG,"nonclick"); cancelTimer(); startTimer(); } } }); } },2000 * i); } //for end } //oncreate end //start timer function voID startTimer() { cTimer = new CountDownTimer(5000,1000) { public voID onTick(long millisUntilFinished) { Log.d(TAG,"ontick"); } public voID onFinish() { Log.d(TAG,"timer onfinish"); } }; cTimer.start(); } //cancel timer voID cancelTimer() { Log.d(TAG,"canceltimer"); if (cTimer != null) cTimer.cancel(); }
3个不同的线程 *** 作但没有工作.其中一个屏幕进入黑屏.其他两个没有阻止循环的进行.
runnable version for ( int i = 0; i< 20 ; i++) { Log.d(TAG,"for i2="+ i); final int finali = i; final relativeLayout finalRLayout = rLayout; final Runnable r=new Runnable() { @OverrIDe public voID run() { Log.d(TAG,"for finali2="+ finali); TrainingObject trainingObject = new TrainingObject(); trainingObject = trainingObjectList.get(finali); objectCount = 2;//test icin Log.d(TAG,"testicin trainingobjectID: "+trainingObject.getTrainingobjectID()); object = dbHandler.getobjectObject(trainingObject.getTrainingobjectAnswer()); relativeLayout.LayoutParams rLayParams = new relativeLayout.LayoutParams(140,140); rLayParams.addRule(relativeLayout.AliGN_PARENT_left); rLayParams.addRule(relativeLayout.CENTER_IN_PARENT); imgBytes = object.getobjectimageBlob(); bmp = BitmapFactory.decodeByteArray(imgBytes,imgBytes.length); imageAnswer.setimageBitmap(bmp); imageAnswer.setTag(trainingObject.getTrainingobjectAnswer()); imageAnswer.setID(R.ID.imgAnswer); finalRLayout.removeAllVIEws(); finalRLayout.addVIEw(imageAnswer,rLayParams); } }; Log.d(TAG,"3000i2 sonrasi"); handler.postDelayed(r,6000 * i); imageAnswer.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if (vIEw == findVIEwByID(R.ID.imgAnswer)) { Log.d(TAG,"clicked");handler.removeCallbacks(r); handler.postDelayed(r,0); //PUT IN CODE HERE TO GET NEXT IMAGE } else{ Log.d(TAG,"nonclick"); handler.removeCallbacks(r); handler.postDelayed(r,6000); } } }); }//for end解决方法 您可以简单地使用handler来实现您的目的,您应该做的是创建一个runnable并在处理程序之外声明它然后在postDelayed处理程序中使用它的名称,如果用户单击,只需删除所有处理程序回调:
// first create runnableRunnable r = new Runnable{ public voID run(){ //do what you need to do after 5 seconds }}// then create handlerHandler mHandler = new Handler();// then in your onCreate methodnCreate() { for (int i = 0; i < 2; i++) { handler.postDelayed(new Runnable() { public voID run() { imageAnswer.setimageBitmap(bmp); imageAnswer.setID(R.ID.imgAnswer); //adding image to screen finalRLayout.removeAllVIEws(); finalRLayout.addVIEw(imageAnswer,rLayParams); //starting handler,run again timer mHandler.postDelayed(r,5000); //clickListerner imageAnswer.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { //if clicks true,cancel timer and go to next iteration of for loop if (vIEw == findVIEwByID(R.ID.imgAnswer)) { Log.d(TAG,"clicked"); //PUT IN CODE HERE TO GET NEXT IMAGE myHandler.removeCallbacks(r); } else { //if not true,run again timer for last chance Log.d(TAG,"nonclick"); // stop the handler myHandler.removeCallbacks(r); // run a new one mHandler.postDelayed(r,5000); } } }); } },2000 * i); } //for end } //oncreate end总结
以上是内存溢出为你收集整理的Android for循环和延迟不同步全部内容,希望文章能够帮你解决Android for循环和延迟不同步所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)