简体   繁体   中英

How to change layout background dynamically

I am having problem in my code.I am trying to change the layout background of my app every second.I used Thread in this code.I've searched the site but I couldn't find anything useful.Here is the code.

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

}

From the answer to this question Change Layout background continuously ...

Try using a Handler . Example...

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

Try this:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM