Android播放循环播放本地图片

ImageSwitcher方便快捷

 
 private ImageSwitcher mImagSwitcher;
 int[] images = {R.drawable.return2, R.drawable.timg,R.drawable.ic_launcher};

 super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        mImagSwitcher = (ImageSwitcher) findViewById(R.id.imagSwitcher);

        mImagSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                // makeView返回的是当前需要显示的ImageView控件,用于填充进ImageSwitcher中
                return new ImageView(Main2Activity.this);
            }
        });

        mImagSwitcher.postDelayed(new Runnable() {
            int currentIndex= 0;
            @Override
            public void run() {
                mImagSwitcher.setImageResource(images[currentIndex]);
                if(currentIndex ==(images.length - 1))
                    currentIndex = 0;
                else
                    currentIndex++;
                mImagSwitcher.postDelayed(this,1000);
            }
        },2000);

你可能感兴趣的:(Android)