无限轮播自动播加动态添加小圆点

时间:2022-03-21 20:44:08

主方法
package com.example.wxlb;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private ViewPager vp;
private LinearLayout ll;
private List<String> list;
private List<ImageView> images;
private Handler myHandler=new Handler(){

    public void handleMessage(Message msg) {
        //获取当前正在显示的页面
        int index=vp.getCurrentItem();
        vp.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);
    vp = (ViewPager) findViewById(R.id.vp);
    ll = (LinearLayout) findViewById(R.id.lin_bottom);
    list = new ArrayList<String>();
    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();
    vp.setAdapter(new ImagePager(this,list));
    vp.setCurrentItem(list.size()*10);
    myHandler.sendEmptyMessageDelayed(1,2000);
}
public void setSelectedpoint(int index){
    for (int i=0;i<list.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);
        ll.addView(imgpoint,params);
        images.add(imgpoint);
    }
}

}
Imageloder类
package com.example.wxlb;

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;

/**
* author:Created by WangZhiQiang on 2017/10/17.
*/

public class ImagePager extends PagerAdapter {
Context context;
List 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);
    ImageLoader.getInstance().displayImage(list.get(position%list.size()),imageView,Myapp.getImageOptions());
    container.addView(imageView);
    return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
}

}

图片适配器类
package com.example.wxlb;

import android.app.Application;
import android.graphics.Bitmap;

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;

/**
* author:Created by WangZhiQiang on 2017/10/17.
*/

public class Myapp extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this).memoryCacheExtraOptions(480, 800)//缓存图片最大的长和宽
.threadPoolSize(2)//线程池的数量
.threadPriority(4)
.memoryCacheSize(2*1024*1024)//设置内存缓存区大
.writeDebugLogs()//打印日志内容
.diskCacheSize(20*1024*1024)//设置sd卡缓存区大小
.diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理
.build();
ImageLoader.getInstance().init(build);
}
public static DisplayImageOptions getImageOptions(){

    DisplayImageOptions optionsoptions=new DisplayImageOptions.Builder()
            .cacheInMemory(true)//使用内存缓存
            .cacheOnDisk(true)//使用磁盘缓存
            .bitmapConfig(Bitmap.Config.RGB_565)//设置图片格式
            .build();

    return optionsoptions;

}

}

**选中的颜色**
<?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>
**布局**
<?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.wxlb.MainActivity">
<android.support.v4.view.ViewPager  android:layout_width="match_parent" android:id="@+id/vp" android:layout_height="200dp"></android.support.v4.view.ViewPager>
    <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/lin_bottom" android:layout_below="@+id/vp" android:gravity="center" android:layout_marginBottom="7dp" ></LinearLayout>
</RelativeLayout>