位图壁纸每10秒更换一次android

时间:2013-03-30 13:02:35

标签: android wallpaper

您好我正在尝试创建位图壁纸。但是这个位图每10秒就会改变一次。我怎么能做到这一点?

这就是我的尝试:

 // I have declared 
 int[] images = {R.drawable.donna, R.drawable.donna1, R.drawable.marian,
        R.drawable.marian1, R.drawable.marian};


 Handler mHandler = new Handler();

 ImageView imgView;

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

    imgView =  (ImageView) findViewById(R.id.imageView1);

                new Thread(new Runnable() {
                    public void run() {
                        // TODO Auto-generated method stub
                        while (true) {
                            try {
                                Thread.sleep(10000);
                                mHandler.post(new Runnable() {

                                    public void run() {
                                        // TODO Auto-generated method stub
                                        // Write your code here to update the UI.
                                        Random ran = new Random();
                                        imgView.setImageResource(images[ran.nextInt(images.length)]);
                                    }
                                });
                            } catch (Exception e) {
                                // TODO: handle exception
                            }
                        }
                    }
                }).start(); 

但我的问题是如何整合功能/方法将其设置为壁纸?

真的很感激任何帮助。感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用postDelayed()在指定的时间范围内更改图片:

Handler mHandler = new Handler(); 
Runnable __runnable = new Runnable()
{
    @Override
    public void run() 
    {
        Random ran = new Random();
        imgView.setImageResource(images[ran.nextInt(images.length)]);
        mHandler.postDelayed(this, 10000);              
    }
};
new Thread(__runnable).start();

关于第二个问题,请参阅以下链接:

how to set image as wallpaper from the ImageViev