import android.content.Context;
import android.graphics.Bitmap;
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 com.nostra13.universalimageloader.core.assist.ImageScaleType;
import java.io.File;
/**
* Created by e531 on 2017/11/13.
*/
public class ImageUtils {
/**
* 对ImageLoader进行初使化
* @param context
*/
public static void initImageLoader(Context context){
File cacheFile=context.getExternalCacheDir();//android认为的缓存目录
ImageLoaderConfiguration configuration=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(configuration);
}
/**
* 得到图片的显示设置类
* @return
*/
public static DisplayImageOptions getImageOptions(){
DisplayImageOptions options=new DisplayImageOptions.Builder()
.cacheInMemory(true)//使用内存缓存
.cacheOnDisk(true)//使用磁盘缓存
.bitmapConfig(Bitmap.Config.RGB_565)//设置图片的色彩模式
.imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放方式
.build();
return options;
}
}
创建一个utils,建立一个ImageUtils初始化,
2.public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
//进行初使化
ImageUtils.initImageLoader(this);
}
}
3.
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
import cuishihao.bwie.com.lianxi2.utils.ImageUtils;
/**
* Created by e531 on 2017/11/13.
*/
public class MyPagerAdapter extends PagerAdapter{
private Context context;
// private int[] imgArray=new int[]{R.mipmap.a0,R.mipmap.a1,R.mipmap.a2};
//用于存放网络图片的url地址
private List<String> imgUrl;
public MyPagerAdapter(Context context,List<String> imgUrl) {
this.context = context;
this.imgUrl=imgUrl;
}
//返回页面的数量
@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);
//0,1,2
// imageView.setImageResource(imgArray[position%imgArray.length]);
//使用ImageLoader下载图片
ImageLoader.getInstance().displayImage(imgUrl.get(position%imgUrl.size()),
imageView, ImageUtils.getImageOptions());
//添加到容器
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
4.
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;
public class MainActivity extends AppCompatActivity {
private Handler myHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
//获取当前正在显示的页面
int index=viewPager.getCurrentItem();
viewPager.setCurrentItem(index+1);
//改变小圆点
setSelectedPoint(index+1);
//延迟发送消息
sendEmptyMessageDelayed(1,2000);
}
};
private ViewPager viewPager;
private LinearLayout linPoint;
private ImageView[] imgArray;
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.vp);
linPoint = (LinearLayout) findViewById(R.id.linpoint);
//数据.....装的是path路径
list = new ArrayList<>();
list.add("https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1510576227&di=8c199118d3ece98e0565e9ae0c751894&src=http://www.5577.com/up/2017-8/15029516238492643.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510585834296&di=a42b00c2d1c5b2903960ecc5c391a5a0&imgtype=0&src=http%3A%2F%2Fwww.5577.com%2Fup%2F2017-8%2F15029516239761152.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510585834296&di=fdb5e7f458f947ad1cdb3154a152de6e&imgtype=0&src=http%3A%2F%2Fwx1.sinaimg.cn%2Forj480%2F006Keh8Jly1fidr0yrc7xj30hs0a0jrl.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510585834295&di=a9198d0f22ff74259f71557bee091d6f&imgtype=0&src=http%3A%2F%2Fwww.xz7.com%2Fup%2F2017-8%2F201781085651764760.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510585834295&di=a2098f90037db615b7fef2ae0f100173&imgtype=0&src=http%3A%2F%2F1.pic.pc6.com%2Fthumb%2Fup%2F2017-08%2F15016384112136643_600_0.jpg");
imgArray=new ImageView[list.size()];
viewPager.setAdapter(new MyPagerAdapter(this,list));
//使页面 可以向左滑动
viewPager.setCurrentItem(list.size()*100);
//实现自动播放
myHandler.sendEmptyMessageDelayed(1,2000);
initPoint();
}
//0,1,2,3,4,......Integer.maxvalue
public void setSelectedPoint(int pageIndex){
//循环imageview控件
//计算要选中小圆点的下标
int selectedIndex=pageIndex%list.size();
for(int i=0;i<list.size();i++){
if(i==selectedIndex){
imgArray[i].setImageResource(R.drawable.point_selected);
}else{
imgArray[i].setImageResource(R.drawable.point_un_selected);
}
}
}
/**
* 初使化小圆点
*/
private void initPoint() {
for(int i=0;i<list.size();i++){
//第一步
ImageView img=new ImageView(this);
//图片的缩放方式
img.setScaleType(ImageView.ScaleType.FIT_XY);
if(i==0){
//使用shape画小圆点
img.setImageResource(R.drawable.point_un_selected);
}else{
img.setImageResource(R.drawable.point_un_selected);
}
//img对象 设置控件的宽度与高度
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(30,30);
//设置右间隔 10px
params.setMargins(0,0,10,0);
//添加到容器中
linPoint.addView(img,params);//第三步
//添加到数组中
imgArray[i]=img;
}
}
}
5.创建视图
<?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"
tools:context="cuishihao.bwie.com.lianxi2.MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/vp"></android.support.v4.view.ViewPager>
<!-- 用来存放 小圆点 Imageview viewgroup.addView -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/linpoint"
android:gravity="center"
android:layout_alignBottom="@+id/vp">
</LinearLayout>
</RelativeLayout>
6. 自动轮播时 小圆点的颜色,在value中
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources></resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
7. 在drawable中 建立两个 一个是轮播选中是 一个是没选中
选中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="#ff0000"></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>
8.在权限中
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<application
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"
android:name=".MyApplication"
>
还要记得导入imaged的jar包