更改图像环路 - 机器人环路、机器人、图像

由网友(一個人想著一個人)分享简介:我不知道为什么我仍然无法找出一个办法做到这一点。虽然这看起来很简单,我花了我整整一天了这一点。但不能这样做。I'm wondering why still I couldn't to figure out a way to do this. Although it seems like very simple, I...

我不知道为什么我仍然无法找出一个办法做到这一点。虽然这看起来很简单,我花了我整整一天了这一点。但不能这样做。

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.

我已经设置骰子的图像。 1.png,2.png,......和6.png。 有一个在我的布局ImageView的。即,

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

在这里,我要迅速改变这种的ImageView看到一些使用上述6图像的视觉/动画。为此,我写了下面这段code。

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

输出:

没有一个可视化的。 ImageView的保持不变,突然改变在循环最后一次迭代。 我认为这是因为循环执行速度非常快。 然后我做了以下。

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

输出:

再没有一个可视化的。 ImageView的保持不变,突然改变在循环最后一次迭代。 然后我做了以下。

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

再没有一个可视化的。 ImageView的保持不变,突然改变在循环最后一次迭代。 有没有在logcat中的任何错误,在这三种情况。

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.

有人可以帮我吗?线程也不会做的伎俩。任何测试的解决方案将是AP preciated。

Can somebody help me with this? Threading also doesn't do the trick. Any tested solution would be appreciated.

谢谢大家。

推荐答案

首先已经有一个动画机器人设置,可以帮助你achive你什么叫做后的 FrameAnimation,这里是如何使用它的一个例子:

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例

您第一,第二和第三code在主线程中运行,你不应该使用睡在主线程!

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

如果您仍想手动设置的图像资源,你可以使用这个code:

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);
阅读全文

相关推荐

最新文章