如何在几秒钟后自动更改背景图像?

时间:2013-06-25 09:34:12

标签: android background-image

我正在开发一个Android应用程序,我希望每5秒后更改一次背景图像。我有可绘制文件夹中的所有图像。

我正在使用我正在使用的代码,但我没有得到输出。作为输出,我得到的图像不会改变。

请帮忙

由于

[CODE]

public class Home extends Activity {

public static int count=0;
int[] drawablearray=new int[]{R.drawable.slider_1,R.drawable.slider_2,R.drawable.slider_3,R.drawable.slider_4,R.drawable.slider_5};

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

    new Handler().postDelayed(new Runnable() {
        public void run() {

           if(count<drawablearray.length){

               Home.this.getWindow().
                   setBackgroundDrawableResource(drawablearray[count]);
               count++;  //<<< increment counter here
            }
           else{
              // reset counter here
              count=0;
            }

          }
      }, 5000);
}
}

4 个答案:

答案 0 :(得分:1)

您可以使用Timer

实现此目的
public class Home extends Activity {

public static int count=0;
int[] drawablearray=new int[]{R.drawable.slider_1,R.drawable.slider_2,R.drawable.slider_3,R.drawable.slider_4,R.drawable.slider_5};
Timer _t;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    lnMain = (LinearLayout) findViewById(R.id.lnMain);
        _t = new Timer();
        _t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                runOnUiThread(new Runnable() // run on ui thread
                {
                    public void run() {
                        if (count < drawablearray.length) {

                            lnMain.setBackgroundDrawable(drawablearray[count]);
                            count = (count + 1) % drawablearray.length;
                        }
                    }
                });
            }
        }, 5000, 5000);
}
}

答案 1 :(得分:0)

final Handler h = new Handler();
Runnable r = new Runnable() {
    public void run() {    
       Home.this.getWindow().setBackgroundDrawableResource(drawablearray[count]);
       count += (count+1)%drawablearray.length;  //<<< increment counter here
       h.postDelayed(this, 5000);    
    }
 };

现在称为

h.postDelayed(r, 5000);

答案 2 :(得分:0)

答案 3 :(得分:0)

我认为使用xmls会更容易。您可以更改活动主要布局的背景。

尝试这样的事情:

public class Home extends Activity {

public static int count=0;
int[] drawablearray=new int[]{R.drawable.slider_1,R.drawable.slider_2,R.drawable.slider_3,R.drawable.slider_4,R.drawable.slider_5};

LinearLayout ll;

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

    ll = (LinearLayout) findViewByID(R.id.mainlayout) //It depends of the name that you gave to it

    new Handler().postDelayed(new Runnable() {
        public void run() {
               ll.setBackgroundDrawable(drawablearray[count]);
               // or ll.setBackgroundResource(resid) if you want.
               count += (count+1)%drawablearray.length;
          }
      }, 5000);
}
}