setBackgroundResource不设置图像

时间:2013-05-17 04:19:00

标签: java android multithreading

    Handler hnd = new Handler() {

    @Override
    public void handleMessage(Message msg) {

        int id = sequence.get(msg.arg1);

        if(msg.arg1 % 2 == 0) {
            sq.get(id-1).setBackgroundResource(R.drawable.square_show);
        } else {
            sq.get(id-1).setBackgroundResource(R.drawable.square);
        }
    }
};

@Override
public void onResume() {
    super.onResume();

    Thread background = new Thread(new Runnable() {
        public void run() {
            try {
                for(int i = 0; i < sequence.size()-1; i++) {
                    record_tv.setText(""+i);
                    Thread.sleep(200);
                    Message msg = hnd.obtainMessage();
                    msg.arg1 = i;
                    msg.sendToTarget();
                }
            } catch(Throwable t) {

            }
        }
    });

    background.start();
}

[CODE UPDATED]现在它通过第一个循环并停止

你知道为什么第一个runOnUiThread中的代码被执行但是它没有做我想要的吗?

我想要的是:将图像更改为“方形”,等待2秒,将图像更改为“square_show”,等待2秒并重复循环

我现在已经挣扎了一个小时......

6 个答案:

答案 0 :(得分:1)

您可以使用以下代码轻松设置图像。

sq.get(id-1).setImageResource(R.drawable.square_show);
sq.get(id-1).setImageResource(R.drawable.square);

答案 1 :(得分:0)

因为资源在 UI 主题中发生了变化,而您正在使用后台主题。 UI线程正常运行。 使用handlers

public class MainActivity extends Activity {

Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b = (Button) findViewById(R.id.button1);
}
Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {

        if (msg.arg1 % 2 == 0) {
            b.setBackgroundResource(R.drawable.analytic_icon);
        } else {
            b.setBackgroundResource(R.drawable.ic_launcher);
        }
    }
};

@Override
public void onResume() {
    super.onResume();

    Thread background = new Thread(new Runnable() {
        public void run() {
            try {
                for (int i = 0; i < 20; i++) { 
                    Thread.sleep(2000);
                    Message msg = handler.obtainMessage(); 
                    msg.arg1 = i;
                    msg.sendToTarget();
                }
            } catch (Throwable t) {
                // just end the background thread
            }
        }
    });

    background.start();
}
}

答案 2 :(得分:0)

public void show(int size){

    // CICLE THROUGH EACH SQUARE

    for(int i = 0; i <= size-1; i++) {          

        Thread thrd = new Thread() {
            public void run() {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                                           sq.get(id-1).setImageResource(R.drawable.square_show);         
                               //     System.out.println("1st.........."); 
                       try {
                        sleep(2000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                                         sq.get(id-1).setImageResource(R.drawable.square);
                  //     System.out.println("2nd..........");
                       try {
                        sleep(2000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    }
                });




            }
        };

        thrd.start();
    }
}

答案 3 :(得分:0)

这是实现它的错误方法。这可能对你有所帮助。 http://developer.android.com/guide/topics/graphics/2d-graphics.html#tween-animation

答案 4 :(得分:0)

我建议您使用处理程序

int drawablebkg[] ={R.drawable.ic_launcher,R.drawable.icon};
Handler m_handler;
Runnable m_handlerTask ; 
ImageView iv;
iv = (ImageView)findViewById(R.id.imageView1);
m_handler = new Handler(); 
   m_handlerTask = new Runnable()
    {
         @Override 
         public void run() {
             iv.setImageResource(android.R.color.transparent); 
            if(i<2)
            {

              ivsetBackgroundResource(drawablebkg[i]);
              i++;
            }
            else 
              {
                  i=0;
              }
              m_handler.postDelayed(m_handlerTask, 2000);
         }
    };
    m_handlerTask.run();  

活动的onPause()

 @Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

    //_t.cancel();
    m_handler.removeCallbacks(m_handlerTask);
}  

另一种方式

    iv= (ImageView)findViewById(R.id.imageView1);
    AnimationDrawable animation = new AnimationDrawable();
    animation.addFrame(getResources().getDrawable(R.drawable.ic_launcher), 2000);
    iv.setImageResource(android.R.color.transparent); 
    animation.addFrame(getResources().getDrawable(R.drawable.icon), 2000);

    animation.setOneShot(false);

    iv.setBackgroundDrawable(animation);
    //set setBackgroundDrawable(animation) is decprecreated i guess. not sure in which api

    // start the animation!
    animation.start();

另一种方式

在drawable文件夹中定义background.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/ic_launcher" android:duration="2000" />
<item android:drawable="@drawable/icon" android:duration="2000" />
</animation-list>

我的活动onCreate();

ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.setBackgroundResource(R.drawable.background);

AnimationDrawable animation= (AnimationDrawable)loadingRaven.getBackground();
loadingRaven.setImageResource(android.R.color.transparent); 
animation.start();

注意停止动画需要调用animation.stop()

答案 5 :(得分:0)

试试这个,它会起作用:

public void show(final int size) {
    Thread thrd = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i <= size - 1; i++) {
                id = (Integer) sequence.get(i);
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        sq.get(id - 1).setBackgroundResource(
                                R.drawable.square_show);
                    }
                });
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        sq.get(id - 1).setBackgroundResource(
                                R.drawable.square);
                    }
                });
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    thrd.start();
}