2

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

我正在提供我正在使用的代码,但我没有得到输出。作为输出,我得到一个没有变化的静止图像。

请帮忙

谢谢

[代码]

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

4 回答 4

1

You can achieve this using 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);
}
}
于 2013-11-22T08:14:41.820 回答
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);
于 2013-06-25T09:37:51.743 回答
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);
}
}
于 2013-06-25T10:02:33.863 回答
0

为什么不看看 ViewFlipper 类 http://developer.android.com/reference/android/widget/ViewFlipper.html

于 2013-06-25T09:40:05.033 回答