setBackgroundResource不设置图像

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秒并重复循环

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

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

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

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

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

我建议你使用一个处理程序

 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

      

我你的活动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()

因为UI线程中的资源发生了变化,而您正在睡眠后台线程。 UI线程正常运行。 使用处理程序 :

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

试试这个,它会起作用:

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