Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8321885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:10:44+00:00 2026-06-08T23:10:44+00:00

i have a question regarding the refreshment of an imageview. Basically, I have a

  • 0

i have a question regarding the refreshment of an imageview. Basically, I have a button and a dice. I want the dice to rotate a couple of times (to simulate a dice roll) – 0.5 sec, and finally to stop it. The problem is: I can just see the final result. The dice roll simulation is being done, but the change is not being shown (the imageview is not being refreshed). I think it’s my erroreneous understanding of the way this should be implemented.

I isolated the problem in a simple project, a couple of lines of code, you can download it here (in case you have the time to play with it): http://dl.dropbox.com/u/26268461/vrtiKocku_eng.rar

The whole code looks like this:


package com.viscode.vrtiKocku;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class VrtiKockuActivity extends Activity {
    /** Called when the activity is first created. */

    private final Handler mHandler = new Handler(); 
    Random randomGenerator = new Random();          
    private ImageView dice1;                        

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        dice1 = (ImageView)findViewById(R.id.imageView1);   
        mHandler.post(mRollDice);                               
    }

private final Runnable mRollDice = new Runnable() {

    public void run() {
        Button buttonRollDice = (Button) findViewById(R.id.buttonRollDice); 
        buttonRollDice.setOnClickListener(new View.OnClickListener() {      

            public void onClick(View v) {                               
                Integer rand, rollSimulationTimeLimit = 500, rollDuration = 100;;                               

                //simulate dice roll for 500 ms, by 5 rolls at 100 ms
                while (rollDuration < rollSimulationTimeLimit) { 
                    rand = randomGenerator.nextInt(6) + 1;
                    postaviKocku(rand, dice1);

                //  wait and SHOW the simulated roll for 100 ms = rollDuration
                try {
                    Thread.sleep(rollDuration);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                rollDuration += rollDuration;
                }                                               
            }
        }); 
    }
};


//this procedure sets the imageview to the proper image, based on the rolled number
private void postaviKocku(Integer rand, final ImageView dice) {
    switch (rand) {
    case 1:
            dice.setImageDrawable(getResources().getDrawable(R.drawable.one_red));                      
        break;
    case 2:
            dice.setImageDrawable(getResources().getDrawable(R.drawable.two_red));                      
        break;
    case 3:
            dice.setImageDrawable(getResources().getDrawable(R.drawable.three_red));                        
        break;
    case 4:
            dice.setImageDrawable(getResources().getDrawable(R.drawable.four_red));                     
        break;
    case 5:
            dice.setImageDrawable(getResources().getDrawable(R.drawable.five_red));                     
        break;
    case 6:
            dice.setImageDrawable(getResources().getDrawable(R.drawable.six_red));                      
        break;
    default:
        break;
    }
}


}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:10:47+00:00Added an answer on June 8, 2026 at 11:10 pm

    use Handler’s postDelayed instead of Threads..

      package com.viscode.vrtiKocku;
    
    import java.util.Random;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class VrtiKockuActivity extends Activity {
        /** Called when the activity is first created. */
    
        private final Handler mHandler = new Handler(); // handler
        Random randomGenerator = new Random(); // varijabla tipa random
        Boolean kocka1pritisnuta = false; // da li je kocka zakljucana ili ne
        Integer vkocka1; // drzi vrijednost kocke
        private ImageView kocka1; // varijabla imageview-a
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            kocka1 = (ImageView) findViewById(R.id.imageView1); // inicijalizira
                                                                // imageview
            mHandler.post(mBacaj); // pokrece handler
        }
    
        private final Runnable mBacaj = new Runnable() {
    
            public void run() {
                Button buttonBacaj = (Button) findViewById(R.id.buttonBacaj); // veze
                                                                                // button
                                                                                // za
                                                                                // varijablu
                buttonBacaj.setOnClickListener(new View.OnClickListener() { // postavlja
                                                                            // listener
                                                                            // na
                                                                            // dugme
    
                            public void onClick(View v) { // definise click event
                                // TODO Auto-generated method stub
                                Integer rand; // varijabla koja drzi rand broj
                                vrtiKocke(); // pokreni VRCENJE KOCKI
    
                                if (!kocka1pritisnuta) { // ako kocka nije
                                                            // zakljucana...
                                    rand = randomGenerator.nextInt(6) + 1; // generisi
                                                                            // random
                                                                            // broj
                                    vkocka1 = rand; // zapamti vrijednost da mozes
                                                    // racunat poslije
                                    postaviKocku(rand, kocka1,0); // postavi sliku
                                                                // kocke koja
                                                                // odgovara
                                                                // vrijednosti
                                }
                            }
                        });
    
                final ImageView kocka1 = (ImageView) findViewById(R.id.imageView1); // povezi
                                                                                    // imageview
                                                                                    // sa
                                                                                    // kockom
                kocka1.setOnClickListener(new View.OnClickListener() { // postavi
                                                                        // listener
                                                                        // na kocku
                    public void onClick(View v) { // definisi click event za kocku
                        kocka1pritisnuta = pritisniKocku(kocka1pritisnuta, kocka1); // izvrsi
                                                                                    // click
                                                                                    // event
                                                                                    // za
                                                                                    // kocku
                    }
                });
            }
        };
    
        // ova procedura treba da vrti kocke do 500 milisekundi, trajanje je po 100
        // ms, znaci 5x se trabaju okrenuti
        private void vrtiKocke() {
            Integer rand, limit = 1000, trajanje = 200;
            while (trajanje < limit) {
                if (!kocka1pritisnuta) {
                    rand = randomGenerator.nextInt(6) + 1;
                    postaviKocku(rand, kocka1, trajanje);
                }
    
                rand = randomGenerator.nextInt(100);
                // cekaj vrijeme trajanja
    
                trajanje += trajanje;
            }
        }
    
        // ova procedura postavlja vrijednost kocke na onu vrijednost broja koji je
        // dobiven
        private void postaviKocku(final Integer rand, final ImageView kocka, Integer trajanje) {
            //Log.i("kocka", rand+"");
            switch (rand) {
            case 1:
                mHandler.postDelayed(new Runnable() {
                    public void run() {
                        Log.i("kocka", rand+"");
                        kocka.setImageDrawable(getResources().getDrawable(R.drawable.one_red));
                    }
                }, trajanje);
                break;
            case 2:
                mHandler.postDelayed(new Runnable() {
                    public void run() {
                        Log.i("kocka", rand+"");
                        kocka.setImageDrawable(getResources().getDrawable(R.drawable.two_red));
                    }
                }, trajanje);
                break;
            case 3:
                mHandler.postDelayed(new Runnable() {
                    public void run() {
                        Log.i("kocka", rand+"");
                        kocka.setImageDrawable(getResources().getDrawable(R.drawable.three_red));
                    }
                }, trajanje);
                break;
            case 4:
                mHandler.postDelayed(new Runnable() {
                    public void run() {
                        Log.i("kocka", rand+"");
                        kocka.setImageDrawable(getResources().getDrawable(R.drawable.four_red));
                    }
                }, trajanje);
                break;
            case 5:
                mHandler.postDelayed(new Runnable() {
                    public void run() {
                        Log.i("kocka", rand+"");
                        kocka.setImageDrawable(getResources().getDrawable(R.drawable.five_red));
                    }
                }, trajanje);
                break;
            case 6:
                mHandler.postDelayed(new Runnable() {
                    public void run() {
                        Log.i("kocka", rand+"");
                        kocka.setImageDrawable(getResources().getDrawable(R.drawable.six_red));
                    }
                }, trajanje);
                break;
            default:
                break;
            }
        }
    
        // ova procedura samo zakljucava ili otkljucava kocku
        private boolean pritisniKocku(boolean kockapritisnuta, ImageView kocka) {
            if (!kockapritisnuta) {
                kocka.setAlpha(128);
            } else
                kocka.setAlpha(255);
            return !kockapritisnuta;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding class design. I want to have a class that
Hi I have question regarding cin and buffer. I want to make a simple
I have a question regarding Date and Time for MYSQL. Basically I started building
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have question regarding disabling browser caching. I have already found few solutions, and
i have a question regarding the AsyncTask class in android, and why it is
I have a question regarding applying a RTRIM on a ASP:Hyperlink statement. The code
I have a question regarding the proper way to modify a php DateTime object.
I have a question regarding the best practise of handling formated text when using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.