实现后的效果:
1.布局:
<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="${relativePackage}.${activityClass}" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/viewpager"
android:background="#33000000"
android:orientation="vertical" >
<TextView
android:id="@+id/image_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@android:color/white"
android:textSize="18sp" />
<LinearLayout
android:orientation="horizontal"
android:id="@+id/point_group"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
2.ViewPager的适配器:
private class MyPagerAdapter extends PagerAdapter {
/**
* 获得页面的总数
*/
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
/**
* 获得相应位置上的view container view的容器,其实就是viewpager自身 position 相应的位置
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
// 给 container 添加一个view
container.addView(imageList.get(position % imageList.size()));
// 返回一个和该view相对的object
return imageList.get(position % imageList.size());
}
/**
* 判断 view和object的对应关系
*/
@Override
public boolean isViewFromObject(View view, Object object) {
if (view == object) {
return true;
} else {
return false;
}
}
/**
* 销毁对应位置上的object
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
object = null;
}
}
3.ViewPager的监听器:
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
/**
*
* 页面切换后调用
*
* @param position
*/
@Override
public void onPageSelected(int position) {
position = position%imageList.size();
//设置文字描述内容
imageDesc.setText(imageDescriptions[position]);
//改变指示点的状态
//把当前点enbale 为true
pointGroup.getChildAt(position).setEnabled(true);
//把上一个点设为false
pointGroup.getChildAt(lastPosition).setEnabled(false);
lastPosition = position;
}
/**
* 页面正在滑动的时候,回调
*/
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
/**
* 当页面状态发生变化的时候,回调
*/
@Override
public void onPageScrollStateChanged(int state) {
}
});
4.Handler自动:
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
//让viewPager 滑动到下一页
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
if(isRunning){
handler.sendEmptyMessageDelayed(0, 2000);
}
};
};
整个逻辑:
package com.dystu.adverdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private ViewPager viewPager;
private LinearLayout pointGroup;
private TextView imageDesc;
// 图片资源ID
private final int[] imageIds = { R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d, R.drawable.e };
// 图片标题集合
private final String[] imageDescriptions = { "巩俐不低俗,我就不能低俗",
"扑树又回来啦!再唱经典老歌引万人大合唱", "揭秘北京电影如何升级", "乐视网TV版大派送", "热血屌丝的反杀" };
private ArrayList<ImageView> imageList;
/**
* 上一个页面的位置
*/
protected int lastPosition;
MyPagerAdapter pageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
pointGroup = (LinearLayout) findViewById(R.id.point_group);
imageDesc = (TextView) findViewById(R.id.image_desc);
imageList = new ArrayList<ImageView>();
imageDesc.setText(imageDescriptions[0]);
for (int i = 0; i < imageIds.length; i++) {
ImageView image = new ImageView(this);
image.setBackgroundResource(imageIds[i]);
imageList.add(image);
//添加指示点
ImageView point =new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.rightMargin = 20;
point.setLayoutParams(params);
point.setBackgroundResource(R.drawable.point_bg);
if(i==0){
point.setEnabled(true);
}else{
point.setEnabled(false);
}
pointGroup.addView(point);
}
pageAdapter = new MyPagerAdapter();
viewPager.setAdapter(pageAdapter);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
/**
*
* 页面切换后调用
*
* @param position
*/
@Override
public void onPageSelected(int position) {
position = position%imageList.size();
//设置文字描述内容
imageDesc.setText(imageDescriptions[position]);
//改变指示点的状态
//把当前点enbale 为true
pointGroup.getChildAt(position).setEnabled(true);
//把上一个点设为false
pointGroup.getChildAt(lastPosition).setEnabled(false);
lastPosition = position;
}
/**
* 页面正在滑动的时候,回调
*/
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
/**
* 当页面状态发生变化的时候,回调
*/
@Override
public void onPageScrollStateChanged(int state) {
}
});
/*
* 自动循环:
* 1、定时器:Timer
* 2、开子线程 while true 循环
* 3、ColckManager
* 4、 用handler 发送延时信息,实现循环
*/
isRunning = true;
handler.sendEmptyMessageDelayed(0, 2000);
}
/**
* 判断是否自动滚动
*/
private boolean isRunning = false;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
//让viewPager 滑动到下一页
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
if(isRunning){
handler.sendEmptyMessageDelayed(0, 2000);
}
};
};
protected void onDestroy() {
isRunning = false;
};
private class MyPagerAdapter extends PagerAdapter {
/**
* 获得页面的总数
*/
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
/**
* 获得相应位置上的view container view的容器,其实就是viewpager自身 position 相应的位置
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
// 给 container 添加一个view
container.addView(imageList.get(position % imageList.size()));
// 返回一个和该view相对的object
return imageList.get(position % imageList.size());
}
/**
* 判断 view和object的对应关系
*/
@Override
public boolean isViewFromObject(View view, Object object) {
if (view == object) {
return true;
} else {
return false;
}
}
/**
* 销毁对应位置上的object
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
object = null;
}
}
}
原点创建一个shape文件即可,状态不同,用selector.