2014-05-16 126 views
0

我在我的代码中遇到了问题。我试图每隔一秒更改我的应用程序的布局背景。我在此代码中使用了线程。我搜索了该网站,但无法找到任何有用的东西。这里是代码。如何动态更改布局背景

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.LinearLayout; 

public class MainActivity extends Activity { 

    //private Bitmap open, close; 
    private LinearLayout myL; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     myL = (LinearLayout) findViewById(R.id.LinearLayout2); 

     // myL=(LinearLayout) findViewById(R.id.LinearLayout2); 

     //close = BitmapFactory.decodeResource(getResources(), R.drawable.kapa); 
     //open = BitmapFactory.decodeResource(getResources(), R.drawable.ac); 

    } 

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

     Thread th = new Thread() { 
      public void run() { 

       while (true) { 
        myL.setBackgroundResource(R.drawable.kapa); 

        try { 
         sleep(1000); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

        myL.setBackgroundResource(R.drawable.ac); 

        try { 
         sleep(1000); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

       } 
      } 
     }; 
     th.start(); 
    } 

} 
+0

问题是什么? – donfuxx

+0

UI相关操作必须在主线程上执行。设置背景时使用'runOnUiThread()'。 – dharms

回答

0

试试这个:

public class MainActivity extends Activity { 

//private Bitmap open, close; 
private LinearLayout myL; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    myL = (LinearLayout) findViewById(R.id.LinearLayout2); 

    // myL=(LinearLayout) findViewById(R.id.LinearLayout2); 

    //close = BitmapFactory.decodeResource(getResources(), R.drawable.kapa); 
    //open = BitmapFactory.decodeResource(getResources(), R.drawable.ac); 

} 

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

    Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 

       while(true) { 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

       MainActivity.this.runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        myL.setBackgroundResource(R.drawable.kapa); 

       } 
      }); 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

       MainActivity.this.runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         myL.setBackgroundResource(R.drawable.ac); 

        } 
       }); 
      } 
      } 
     }; 
     new Thread(runnable).start(); 
} 

} 
+0

感谢您的帮助。它完美的作品。 – sahimgiray

+0

@sahimgiray如果解决方案工作,请接受解决方案。 – araut

1

从这个问题的答案Change Layout background continuously ...

尝试使用Handler。例如...

public class MainActivity extends Activity { 

    final int CHANGE_BG_RES = 1; 
    final int RESOURCE_1 = R.drawable.kapa; 
    final int RESOURCE_2 = R.drawable.ac; 
    private LinearLayout myL; 

    private Handler handler = new Handler() { 
     public void handleMessage(Message msg) { 
      if (CHANGE_BG_RES == msg.what) { 
       int res = msg.arg1; 
       myL.setBackgroundResource(res); 
       int nextRes; 
       if (RESOURCE_1 == res) 
        nextRes = RESOURCE_2; 
       else 
        nextRes = RESOURCE_1; 
       Message m = obtainMessage (CHANGE_BG_RES, nextRes, 0, null); 
       sendMessageDelayed(m, 1000); 
      } 
     } 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     myL = (LinearLayout) findViewById(R.id.LinearLayout2); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     Message m = handler.obtainMessage(CHANGE_BG_RES, RESOURCE_1, 0, null); 
     handler.sendMessageDelayed(m, 1000); 
    } 
}