Android播放循环播放本地图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiyangyang8110/article/details/83443500

ImageSwitcher方便快捷

 <ImageSwitcher
        android:id="@+id/imagSwitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
 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);

猜你喜欢

转载自blog.csdn.net/xiyangyang8110/article/details/83443500
今日推荐