private LinearLayout layout;
// 获取的图片
String[] picUrls = new String[] {
"http://172.17.29.120/localuser/ljy/shop/bannerpic/201407310157304712_x.jpg",
"http://172.17.29.120/localuser/ljy/shop/bannerpic/1458525609380.jpg",
"http://172.17.29.120/localuser/ljy/shop/bannerpic/20130428084719999.jpg" };
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) {
int currentItem = viewPager.getCurrentItem();
currentItem++;
viewPager.setCurrentItem(currentItem);
// 继续轮播
startRool();
}
};
};
private ViewPager viewPager;
private LinearLayout ll_dots;
private ArrayList<ImageView> dotsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
ll_dots = (LinearLayout) findViewById(R.id.ly_bojv);
// 初始化小点
initDots();
// 设置适配器
viewPager.setAdapter(new Myviewpage(this, picUrls, handler));
// 设置当前页
viewPager.setCurrentItem(picUrls.length * 10000);
// 设置ViewPager的监听
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
for (int i = 0; i < picUrls.length; i++) {
if (i == position % picUrls.length) {
dotsList.get(i).setImageDrawable(
getResources().getDrawable(
R.drawable.dots_focuse));
} else {
dotsList.get(i).setImageDrawable(
getResources().getDrawable(
R.drawable.dots_normal));
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
startRool();
}
private void startRool() {
// 开始轮播
handler.sendEmptyMessageDelayed(0, 2000);
}
/**
* 初始化小点
*/
@SuppressLint("NewApi")
private void initDots() {
dotsList = new ArrayList<ImageView>();
dotsList.clear();
// 移除小点
ll_dots.removeAllViews();
for (int i = 0; i < picUrls.length; i++) {
ImageView imageView = new ImageView(this);
Drawable drawable = null;
if (i == 0) {
// 亮色图片
drawable = getResources().getDrawable(R.drawable.dots_focuse);
} else {
drawable = getResources().getDrawable(R.drawable.dots_normal);
}
imageView.setImageDrawable(drawable);
// 考虑屏幕适配
LayoutParams params = new LayoutParams(dip2px(this, 10), dip2px(
this, 10));
params.setMargins(dip2px(this, 5), 0, dip2px(this, 5), 0);
ll_dots.addView(imageView, params);
// 将小点的控件添加到集合中
dotsList.add(imageView);
}
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
@Override
protected void onDestroy() {
// 移除所有的handler信息
handler.removeCallbacksAndMessages(null);
super.onDestroy();
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ viewpager的适配器~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class Myviewpage extends PagerAdapter {
private String[] urls;
private Context context;
private Handler handler;
public Myviewpage(Context context, String[] urls, Handler handler) {
this.handler = handler;
this.context = context;
this.urls = urls;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup Container, int position) {
// ImageView
ImageView imageView = new ImageView(context);
BitmapUtils bitmapUtils = new BitmapUtils(context);
bitmapUtils.display(imageView, urls[position % urls.length]);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 移除handler消息
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_UP:
handler.sendEmptyMessageDelayed(0, 2000);
break;
case MotionEvent.ACTION_CANCEL:
handler.sendEmptyMessageDelayed(0, 2000);
break;
default:
break;
}
// 处理这个事件
return true;
}
});
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="150dp" />
<LinearLayout
android:id="@+id/ly_bojv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/viewpager"
android:gravity="center" >
</LinearLayout>
~~~~~~~~~~~~~~~~~~~~~xml drawable~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·
//两个 drawable的辅助类
//第一个
//xml 命名 dots_focuse.xml
//内容
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ff0000" />
<corners android:radius="8dp" />
</shape>
//第二个
//xml 命名 dots_normal.xml
//内容
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#88000000" />
<corners android:radius="8dp" />
</shape>