//添加的权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
android:name=".MyApplication" //导入imageLoaderjar包
activity_main布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.samsung.sfl10211.MainActivity"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/vp"></android.support.v4.view.ViewPager> <!--android:layout_alignBottom: 控件的下边对齐--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/lin_bottom" android:layout_alignBottom="@+id/vp" android:gravity="center" android:layout_marginBottom="7dp"></LinearLayout> </RelativeLayout>
drawable下新建XML文件,,rb_select(选中的小圆点)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#f00"></solid> </shape>
drawable下新建XML文件,,rb_un_select(未选中的小圆点)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#ccc"></solid> </shape>
MainActivity 类
public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private List<String> list; private LinearLayout linearLayout; //存放小圆点控件的集合 private List<ImageView> images; private Handler myHandler=new Handler(){ @Override public void handleMessage(Message msg) { //获取当前正在显示的页面 int index=viewPager.getCurrentItem(); viewPager.setCurrentItem(index+1); //改变小圆点 setSelectedPoint((index+1)%list.size()); //延迟发送消息 sendEmptyMessageDelayed(1,2000); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.vp); linearLayout = (LinearLayout) findViewById(R.id.lin_bottom); //数据.....装的是path路径 list = new ArrayList<>(); list.add("http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg"); list.add("http://pic2.nipic.com/20090424/1468853_230119053_2.jpg"); list.add("http://img3.3lian.com/2013/s1/20/d/57.jpg"); list.add("http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg"); list.add("http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg"); //初始化小圆点 initDoc(); //设置适配器 viewPager.setAdapter(new ImagePager(this,list)); //设置当前要显示的页面 viewPager.setCurrentItem(list.size()*10); //实现自动播放 myHandler.sendEmptyMessageDelayed(1,2000); } /** * 改变小圆点图片 * @param index 页面的下标 */ public void setSelectedPoint(int index){ for(int i=0;i<images.size();i++){ if(i==index){ images.get(i).setImageResource(R.drawable.rb_select); }else{ images.get(i).setImageResource(R.drawable.rb_un_select); } } } /** * 初始化小圆点 * 动态添加 */ private void initDoc() { images=new ArrayList<>(); for(int i=0;i<list.size();i++){ ImageView imgPoint=new ImageView(this); //设置图片的缩放模式 imgPoint.setScaleType(ImageView.ScaleType.FIT_XY); //设置要显示的图片 if(i==0){ //红色的点 imgPoint.setImageResource(R.drawable.rb_select); }else{ imgPoint.setImageResource(R.drawable.rb_un_select); } //设置宽度与高度 LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(40,40); params.setMargins(10,0,10,0); //添加到底部容器中 linearLayout.addView(imgPoint,params); //添加到集合中 images.add(imgPoint); } } }
ImagePager 类
public class ImagePager extends PagerAdapter { Context context; List<String> list; public ImagePager(Context context, List<String> list) { this.context = context; this.list = list; } @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); //加载网络的图片list.get(position%list.size()) ImageLoader.getInstance().displayImage(list.get(position%list.size()),imageView,ImageLoaerUtil.getImageOptions()); //添加到容器中 container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }ImageLoaerUtil 类
public class ImageLoaerUtil { /** * ImageLoader的配置 * @param context */ public static void initConfig(Context context) { //配置 // File cacheFile=context.getExternalCacheDir(); File cacheFile= new File(Environment.getExternalStorageDirectory()+"/"+"imgages"); ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(context) .memoryCacheExtraOptions(480, 800)//缓存图片最大的长和宽 .threadPoolSize(2)//线程池的数量 .threadPriority(4) .memoryCacheSize(2*1024*1024)//设置内存缓存区大小 .diskCacheSize(20*1024*1024)//设置sd卡缓存区大小 .diskCache(new UnlimitedDiscCache(cacheFile))//自定义缓存目录 .writeDebugLogs()//打印日志内容 .diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理 .build(); ImageLoader.getInstance().init(config); } /** * 获取图片设置类 * @return */ public static DisplayImageOptions getImageOptions(){ DisplayImageOptions optionsoptions=new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片格式 .build(); return optionsoptions; } }MyApplication 类
public class MyApplication extends Application{ @Override public void onCreate() { super.onCreate(); ImageLoaerUtil.initConfig(this); } }