android — Android for loop и delay не синхронны" />

Android for loop и delay не синхронны

SHORT SUMMARY

I have an activity game. User will click images:

First level of game

Second level of game

User has 5 seconds to click and to choose correct one. After 5 seconds, the correct imageview will be shown differently to draw attention and again after 5 seconds, user will see the next level.

user can click without waiting 5 seconds, so it should go immediately to next level when clicked.

I am creating view and imageviews dynamically. Imageviews are comng from local database sqlite as bytes.

My method is creating inside oncreate method. And i use for or while loop. Each iteration is a level of game. Each level has different imageviews and different count of imageviews.

For delaying 5 + 5 seconds, i had to use handlers, threads, countdowntimers. Each one caused problems.

While using handlers, when user clicked before 5 seconds, i couldnot terminate current session.

I dont know if this is the right way.

You can see my codes below:

LONG EXPLANATION AND CODES

timer:

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 different thread operations but did not work. one of them made screen went into black screen. other two did not block loop's going.

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
7 голосов | спросил user3575963 15 Mayam16 2016, 07:01:46

0 ответов


Похожие вопросы

Популярные теги

security × 330linux × 316macos × 2827 × 268performance × 244command-line × 241sql-server × 235joomla-3.x × 222java × 189c++ × 186windows × 180cisco × 168bash × 158c# × 142gmail × 139arduino-uno × 139javascript × 134ssh × 133seo × 132mysql × 132