0

您好我正在尝试创建位图壁纸。但此位图每 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(); 

但我的问题是如何集成将其设置为墙纸的功能/方法?

任何帮助都非常感谢。谢谢。

4

1 回答 1

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

对于您的第二个问题,请参阅以下链接:

如何从 ImageViev 将图像设置为墙纸

于 2013-03-30T13:08:08.517 回答