activity_main
<?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>
point_selected
<?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>
point_un_selected
<?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>
ImageloaderUtil
package animtest.com.example.e531.zixun_tostudent;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
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;
/**
*
*/
public class ImageloaderUtil {
/**
* 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
package animtest.com.example.e531.zixun_tostudent;
import android.app.Application;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.File;
/**
* Created by e531 on 2017/10/14.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//进行全局初使化
ImageloaderUtil.initConfig(this);
}
}
ImagePager
package animtest.com.example.e531.zixun_tostudent;
import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
*/
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;
}
//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
package animtest.com.example.e531.zixun_tostudent;
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.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));
//设置当前要显示的页面
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);
}
}
}