首先是布局页面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="animtest.com.example.e531.zixun_tostudent.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>
ImagePager页面的代码
首相继承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;
}
//0~getCount返回的值-1
@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,ImageloaderUtil.getImageOptions());
//添加到容器
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
MainActivity中的代码应用
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));
//设置当前要显示的页面
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.point_selected);
}else{
images.get(i).setImageResource(R.drawable.point_un_selected);
}
}
}
/**
* 初始化小圆点
* 动态添加
*/
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.point_selected);
}else{
imgPoint.setImageResource(R.drawable.point_un_selected);
}
//设置宽度与高度
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(40,40);
params.setMargins(10,0,10,0);
//添加到底部容器中
linearLayout.addView(imgPoint,params);
//添加到集合中
images.add(imgPoint);
}
}
还有小圆点的设置
红色的点
<?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>