实现图片的一个轮转功能
利用Handler和Timer设置的一个图片定时轮转效果
布局文件仅为一个ImageView组件,java代码如下
public class MainActivity extends Activity{ private ImageView imageView; int currentNum = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.image); final int[] images={R.drawable.jellyfish,R.drawable.koala,R.drawable.penguins,R.drawable.lighthouse}; final Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==0123){ imageView.setImageResource(images[currentNum++%images.length]); } } }; new Timer().schedule(new TimerTask(){ @Override public void run() { handler.sendEmptyMessage(0123); } },0,1200); } }