2014-05-16 76 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); 
    } 
}