public class MainActivity extends AppCompatActivity { private List<String> imgUrls=new ArrayList<>(); private ViewPager pager; private List<ImageView> lists=new ArrayList<>(); private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { int indexs=pager.getCurrentItem(); indexs++; pager.setCurrentItem(indexs); setSelectItem(indexs%imgUrls.size()); handler.sendEmptyMessageDelayed(1,1000); } }; private LinearLayout linpotn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pager=findViewById(R.id.page); linpotn = findViewById(R.id.linpotn); MyAdapter adapter=new MyAdapter(this,imgUrls); pager.setAdapter(adapter); initDate(); handler.sendEmptyMessageDelayed(1,1000); } private void initDate() { imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151721118&di=649c9a43aed72fbc4d99ec1a031510c6&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F015c7d574b9f8f6ac72525aee98351.jpg"); imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151956771&di=0eb6f306991d24b67a13ceb336f80102&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farchive%2F00613def3f1beb7a35ae136341be2b589bc46a2d.jpg_320x200.jpg"); imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151847685&di=c7a4b5d08ec43fa629bcb690039a7629&imgtype=0&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_080625%2F20080625_2e91a10c444877e88827vri2ZKdGMvQo.jpg"); imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151825129&di=70bf74b87d8a15cb91a2d79f15ed0eaf&imgtype=0&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_081016%2F20081016_fee215664d5740e56c13E2YB8giERFEX.jpg"); imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505746504&di=930c4d677a02328a142d6fa85ed14580&imgtype=jpg&er=1&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_090113%2F20090113_6ac58b42bea94f0b318e1B6BZb5lPZl5.jpg"); //动态小圆点 for (int i = 0; i < imgUrls.size(); i++) { ImageView imageView=new ImageView(this); if (i==0){ imageView.setImageResource(R.drawable.point_selected); }else{ imageView.setImageResource(R.drawable.point_mormal); } linpotn.addView(imageView); lists.add(imageView); } } public void setSelectItem(int selectItem) { for (int i = 0; i < imgUrls.size(); i++) { if (i==selectItem){ lists.get(i).setImageResource(R.drawable.point_selected); }else { lists.get(i).setImageResource(R.drawable.point_mormal); } } }
}
//适配器==========================================================================
class MyAdapter extends PagerAdapter{ private DisplayImageOptions options; private Context context; private List<String> imgUrls; public MyAdapter(Context context, List<String> imgUrls) { this.context = context; this.imgUrls=imgUrls; this.options=new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片 .showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时 .showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 1px=2个字节 ARGB_8888 1px=4个字节 ARGB_4444 1px=2个字节 ALPHA_8 1px=1个字节 .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式 .build(); this.options=new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片 .showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时 .showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 1px=2个字节 ARGB_8888 1px=4个字节 ARGB_4444 1px=2个字节 ALPHA_8 1px=1个字节 .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式 .build(); } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView=new ImageView(context); imageView.setScaleType(ImageView.ScaleType.FIT_XY); ImageLoader.getInstance().displayImage(imgUrls.get(position%imgUrls.size()),imageView,options); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }