Timer的应用

来源:互联网 发布:c语言编程培训 编辑:程序博客网 时间:2024/05/29 08:01
package com.example.jinzhe.timerdemo;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.widget.ImageView;import java.util.Timer;import java.util.TimerTask;public class MainActivity extends AppCompatActivity {    int[] imgs = {R.mipmap.bg03, R.mipmap.bg04, R.mipmap.bg05, R.mipmap.fly01};    private ImageView imageview;    private int num=0;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        final Handler handler=new Handler(){            @Override            public void handleMessage(Message msg) {                super.handleMessage(msg);                if (msg.what==1){                    imageview.setImageResource(imgs[num++]);                    if (num>=imgs.length){                        num=0;                    }                }            }        };        new Timer().schedule(new TimerTask() {            @Override            public void run() {                Message msg=new Message();                msg.what=1;                handler.sendMessage(msg);            }        },1000,500);    }    private void initView() {        imageview= (ImageView) findViewById(R.id.mimageView);    }}
0 0
原创粉丝点击