//MainActivity
package com.bwie.bannerdemo;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private List imgeList;
//定义一个初始页
private int pageIndex = 0;
//定义一个变量记住前一个点位置信息
private int oldPosition = 0;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
vp.setCurrentItem(pageIndex);
}
};
private LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//准备图片数据
imgeList = Arrays.asList("http://cdn.duitang.com/uploads/blog/201308/18/20130818150526_Ru2Bk.thumb.600_0.png",
"http://www.bkill.com/u/info_img/2012-09/02/2012083116140522302.jpg",
"http://www.it165.net/uploadfile/2011/1218/20111218070928328.jpg");
initView();
//轮播
atuoPlay();
}
private void initView() {
vp = (ViewPager) findViewById(R.id.vp);
vp.setAdapter(new MyPagerAdapter());
//获得存放dotView容器
ll = (LinearLayout) findViewById(R.id.ll);
//根据图片的个数生成小圆点
for (int i = 0;i < imgeList.size();++i){
View dotView = new View(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30,30);
//设置控件与控件的间距
params.leftMargin = 10;
params.rightMargin = 10;
dotView.setLayoutParams(params);
if(i == 0)
{
dotView.setBackgroundResource(R.drawable.shape_select);
}else{
dotView.setBackgroundResource(R.drawable.shape_normal);
}
//将点往容器中添加LinearLayout
ll.addView(dotView);
}
//为viewpager添加一个页面改变的监听
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//0,1,2 这个是切换当前那个页面的状态
ll.getChildAt(position % imgeList.size()).setBackgroundResource(R.drawable.shape_select);
//这个是将前一个点状态变更为未选中状态
ll.getChildAt(oldPosition % imgeList.size()).setBackgroundResource(R.drawable.shape_normal);
//更新圆点信息
oldPosition = position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//super.destroyItem(container, position, object); 有bug,需要去掉
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(MainActivity.this);
//加载图片,ImageLoader加载
ImageLoader.getInstance().displayImage(imgeList.get(position % imgeList.size()),imageView,MyApplication.getOptions());
container.addView(imageView);
return imageView;
}
}
public void atuoPlay(){
new Thread(){
@Override
public void run() {
super.run();
while (true){
try { Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}
pageIndex++;
//发送空消息,切换页面
handler.sendEmptyMessage(0);
}
}
}.start();
}
}
//Application类
package com.bwie.bannerdemo;
import android.app.Application;
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;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
String path = Environment.getExternalStorageDirectory()+"/1507DPics";
File cacheDir = new File(path);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.threadPoolSize(3)
.threadPriority(100)
.memoryCacheExtraOptions(480,800)
.memoryCacheSize(2 * 1024 * 1024)
.diskCache(new UnlimitedDiskCache(cacheDir))
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
.build();
ImageLoader.getInstance().init(config);
}
public static DisplayImageOptions getOptions(){
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.showImageOnLoading(R.mipmap.loading)
.showImageOnFail(R.mipmap.ic_error)
.showImageForEmptyUri(R.mipmap.ic_empty)
.build();
return options;
}
}