简短的摘要
我有一个活动游戏.用户将点击图片:
用户有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, 0, 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