轮播图广告的实现内容
1. 让图片滑动起来(ViewPager)
2. 让图片和文字,指示器对应起来
3. 让轮播器无限循环
4. 轮播器自动轮询
实现效果如下:
1.首先是编写布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="160dp" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="5dp"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="#66000000" >
<!--图片说明-->
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
<!--添加小圆点位置,图片指示器-->
<LinearLayout
android:id="@+id/ll_point_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal"
></LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
2.主函数内容:
public class MainActivity2 extends Activity implements ViewPager.OnPageChangeListener{
private ViewPager viewPager;
private TextView textView;
private LinearLayout ll_point_container;
private int[] imageIds;
private String[] contentDescs;
private List<ImageView> imageList;
private int previousSelectedPosition = 0;
boolean isRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
initData();
initAdapter();
viewPager.setOnPageChangeListener(this);
//开启轮播
new Thread(){
@Override
public void run() {
super.run();
isRunning=true;
while(isRunning){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
}
});
}
}
}.start();
}
private void initView() {
viewPager= (ViewPager) findViewById(R.id.viewpager);
textView= (TextView) findViewById(tv_desc);
ll_point_container= (LinearLayout) findViewById(R.id.ll_point_container);
}
private void initData() {
//轮播图资源
imageIds=new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e};
// 文本描述
contentDescs = new String[]{
"巩俐不低俗,我就不能低俗",
"扑树又回来啦!再唱经典老歌引万人大合唱",
"揭秘北京电影如何升级",
"乐视网TV版大派送",
"热血屌丝的反杀"
};
imageList=new ArrayList<ImageView>();
ImageView imageView;
View pointView;
LinearLayout.LayoutParams layoutParmas;
for(int i=0;i<imageIds.length;i++){
imageView=new ImageView(this);
imageView.setBackgroundResource(imageIds[i]);
imageList.add(imageView);
//加上小白点,指示器
pointView=new View(this);
pointView.setBackgroundResource(R.drawable.selector_bg_point);
layoutParmas= new LinearLayout.LayoutParams(10,10);
if(i!=0)layoutParmas.leftMargin=10;
//设置默认所有都不可用
pointView.setEnabled(false);
ll_point_container.addView(pointView,layoutParmas);
}
}
private void initAdapter() {
ll_point_container.getChildAt(0).setEnabled(true);
textView.setText(contentDescs[0]);
previousSelectedPosition=0;
viewPager.setAdapter(new MyAdapter());
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// 滚动时调用
}
@Override
public void onPageSelected(int position) {
// 新的条目被选中时调用
int newPosition = position % imageList.size();
//设置文本
textView.setText(contentDescs[newPosition]);
// 把之前的禁用, 把最新的启用, 更新指示器
ll_point_container.getChildAt(previousSelectedPosition).setEnabled(false);
ll_point_container.getChildAt(newPosition).setEnabled(true);
// 记录之前的位置
previousSelectedPosition = newPosition;
}
@Override
public void onPageScrollStateChanged(int state) {
// 滚动状态变化时调用
}
class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
//1.返回要显示的条目
@Override
public Object instantiateItem(ViewGroup container, int position) {
int newPosition=position%imageList.size();
ImageView imageView=imageList.get(newPosition);
//将view对象添加到container中
container.addView(imageView);
//将View对象返回给框架,适配器
return imageView;
}
//2.销毁条目
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//object要销毁的对象
container.removeView((View) object);
}
//3.指定复用的判断逻辑,为固定写法
@Override
public boolean isViewFromObject(View view, Object object) {
// 当滑动到新的条目, 又返回来, view是否可以被复用.
// 返回判断规则
return view==object;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
isRunning=false;//退出时停止轮播
}
}
通过设置Count为Integer.MAX_VALUE是的图片可以无限轮播。
3.实现圆点指示器的XML文件
selector_bg_point.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_bg_point_enable" android:state_enabled="true"/>
<item android:drawable="@drawable/shape_bg_point_disable" android:state_enabled="false"/>
</selector>
shape_bg_point_disable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="5dp" />
<solid android:color="@android:color/darker_gray" />
</shape>
shape_bg_point_enable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="5dp" />
<solid android:color="#FFFFFF" />
</shape>