Changing images in a Loop - Android

和自甴很熟 提交于 2019-12-06 08:08:52

问题


I'm wondering why still I couldn't to figure out a way to do this. Although it seems like very simple, I spent my entire day for this. But couldn't do that.

I have set of dice images. 1.png,2.png,.... and 6.png. There is an ImageView in my layout. That is,

ImageView dice = (ImageView) findViewById(R.id.imageViewrollingdiceOne);

Here I want to change this imageView rapidly to see a some kind of a visual/animation using above 6 images. For that I wrote following piece of code.

Code 1:

for (int j=0;j<10;j++){
        int randomNum = random.nextInt(6);
            System.out.println("Random Value " + randomNum);
            dice.setImageResource(images[randomNum]);               
    }

Output:

There is not a visual. imageView remains unchanged and suddenly changes at the loop last iteration. I thought that it is because the loop is executing very fast. Then I did the following.

Code 2:

for (int j=0;j<10;j++){
        int randomNum = random.nextInt(6);
            System.out.println("Random Value " + randomNum);
            dice.setImageResource(images[randomNum]);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }               
    }

Output:

Again there is not a visual. imageView remains unchanged and suddenly changes at the loop last iteration. Then I did the following.

Code 3:

final Handler localHandler = new Handler();
    Runnable runnableObject = new Runnable() {
        public void run() {
            final ImageView dice = (ImageView) findViewById(R.id.imageViewrollingdiceOne);
            int randomNum = random.nextInt(6);
            System.out.println("Random Value" + randomNum);
            dice.setImageResource(images[randomNum]);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    };
    for (int j=0;j<10;j++){
        localHandler.postDelayed(runnableObject, 1000);
    }

Again there is not a visual. imageView remains unchanged and suddenly changes at the loop last iteration. There are no any errors in logcat in all three cases.

I found that threading also doesn't do the trick.


回答1:


First of all there is already an animation set in android that can help you achive what you are after it is called FrameAnimation, here is an example on how to use it:

FrameAnimation Example

Your First, second and third code are running in the main thread, you should never use sleep in the main thread !.

If you still want to set the image resource manually you can use this code:

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            int randomNum = random.nextInt(6);
            dice.setImageResource(images[randomNum]);
            handler.postDelayed(this, 500);
        }
    }, 500);



回答2:


This is helpful when you're using list of items and changing according time interval one by one. Initially i value should be 0, size of list depends.

final Handler handler = new Handler();
handler.postDelayed(new Runnable(){
    public void run(){
        if(i<4) {
            galleryImg.setImageResource(myImageList[i]);
            i++;
        } else {
            i=0;
        }
        handler.postDelayed(this, 10000);
    }
}, 500);



回答3:


The reason you're probably not seeing changes is because the final callback to the main UI thread happens after your thread finishes its execution and you only see the final result. Now, this is my (probably poor) understanding and someone more versed in it can probably correct me.

That said you should probably look to using an AnimationDrawable object:

ImageView sImage = (ImageView)findViewById(R.id.imageViewrollingdiceOne);
AnimationDrawable anim = new AnimationDrawable();

for (int j=0;j<6;j++) {
  anim.addFrame(new BitmapDrawable(images[j], 200);
}

sImage.setBackgroundDrawable(anim);
anim.setOneShot(false);
anim.start();

This assumes you've generated images[] with the images in random order already. If you want to change that up put whatever code you need in the for-loop.

What it does is creates an animation object much like how you'd see a gif work with a set delay interval between changes (in this code its 200ms). You can elect to change that number if you wish. The setOneShot call makes sure it loops instead of finishing the animation and stopping on the last image.



来源:https://stackoverflow.com/questions/14733592/changing-images-in-a-loop-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!