在一个循环中改变图像-Android[英] Changing images in a Loop - Android

本文是小编为大家收集整理的关于在一个循环中改变图像-Android的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想知道为什么我仍然无法找到一种方法来做到这一点.尽管看起来很简单,但我花了整整一天的时间为此.但不能那样做.

我有一组骰子图像. 1.png,2.png,....和6.png. 我的布局中有一个图像视图.也就是说,

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

在这里,我想快速更改此图像视图,以使用上述6张图像看到某种视觉/动画.为此,我写了以下代码.

代码1:

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

输出:

没有视觉效果. ImageView保持不变,并突然在循环的最后一次迭代中发生变化. 我认为这是因为循环执行非常快. 然后我做了以下内容.

代码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();
            }               
    }

输出:

再次没有视觉效果. ImageView保持不变,并突然在循环的最后一次迭代中发生变化. 然后我做了以下内容.

代码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);
    }

再次没有视觉效果. ImageView保持不变,并突然在循环的最后一次迭代中发生变化.在所有三种情况下,logcat中没有任何错误.

我发现线程也没有解决问题.

推荐答案

首先,Android中已经有一个动画设置,可以帮助您实现所谓的 frameanimation ,这是有关如何使用它的示例:

fremeanimation示例

您的第一个,第二和第三代码在主线程中运行,您绝不应该在主线程中使用睡眠!

如果您仍然想手动设置图像资源,则可以使用此代码:

    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);

其他推荐答案

当您使用项目列表并按一个时间间隔更改时,这很有帮助.最初i值应为0,列表的大小取决于

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);

其他推荐答案

您可能看不到更改的原因是因为在线程完成执行后,对主UI线程的最终回调发生,并且您只能看到最终结果.现在,这是我的(可能很糟糕的)理解,更精通它的人可能会纠正我.

说您可能应该考虑使用AnimationDrawable对象:

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();

这假设您已经以随机顺序生成了images[].如果您想更改此操作

它所做的是创建一个动画对象,就像您看到gif在更改之间的设置延迟间隔(在此代码中为200ms)的方式一样.如果您愿意,您可以选择更改该号码. setOneShot呼叫确保它循环而不是完成动画并停止最后图像.

本文地址:https://www.itbaoku.cn/post/102592.html

问题描述

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.

推荐答案

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);

其他推荐答案

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);

其他推荐答案

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.