//main方法
import android.os.Handler; import android.os.Message; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import java.util.ArrayList; import java.util.List; /** * * ViewPager 自动无限轮播-前不见头 后不见尾,handler * * 适配器PagerAdapter * 1.getCount:Interger.max_value无限大 * 2.insta...Item:装载页面 1.加载页面的视图 2.返回一个object * 动态生成页面 * * 3.destroy...item:销毁页面 去掉super.des... 移除remove(view) * 3.isViewfromObject: arg1==arg0 * * viewpager默认是加载三页 * 设置当前显示的页面 setCurrentItem(0) * * ImageLoader异步获取图片 * * * */ 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("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"); list.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"); list.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"); list.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"); list.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"); //初始化小圆点 initDoc(); //设置适配器 viewPager.setAdapter(new ImagePager(this,list,myHandler)); //设置当前要显示的页面 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); } } }
//工具包
import android.content.Context; import android.graphics.Bitmap; import android.os.Environment; import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import java.io.File; /** * Created by 知足 on 2017/10/18. */ 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 UnlimitedDiskCache(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; } }
//适配器
import android.content.Context; import android.content.Intent; import android.os.Handler; import android.support.v4.view.PagerAdapter; import android.text.Html; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; /** * Created by 知足 on 2017/10/18. */ public class ImagePager extends PagerAdapter { Context context; List<String> list; Handler mHandler; public ImagePager(Context context, List<String> list, Handler mHandler) { this.context = context; this.list = list; this.mHandler = mHandler; } @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()); imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: Log.d("zzz","down--"); //移除消息 mHandler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_MOVE: Log.d("zzz","move--"); break; case MotionEvent.ACTION_UP: Log.d("zzz","up--"); //再次发送消息 mHandler.sendEmptyMessageDelayed(1,2000); Intent intent=new Intent(context,Main2Activity.class); context.startActivity(intent); break; case MotionEvent.ACTION_CANCEL: Log.d("zzz","cancel--"); //再次发送消息 mHandler.sendEmptyMessageDelayed(1,2000); break; } return true; } }); //添加到容器中 container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }
//Main方法2
public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); } }
//工具包调用
import android.app.Application; /** * Created by 知足 on 2017/10/18. */ public class MyApplication extends Application{ @Override public void onCreate() { super.onCreate(); //进行全局初始化 ImageLoaerUtil.initConfig(this); } }
//设置小圆点颜色
//选中时颜色
<?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>
//不选时颜色
<?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>
//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.example.viewpager.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>
//activity-main2
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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.example.viewpager.Main2Activity"> <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ratingBar" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="114dp" /> </android.support.constraint.ConstraintLayout>
//需要加的权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.viewpager"> <uses-permission android:name="android.permission.INTERNET" /> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Main2Activity"></activity> </application> </manifest>