这篇文章将为大家详细讲解有关android中怎么实现一个自动轮播图效果,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1、准备好需要的轮播图片,图片标题(初始化,声明)。
/**轮播图片*/
private int[] imageIds=new int[]{
R.drawable.ic_launcher,
R.drawable.simple_player_control_focused_holo,
R.drawable.dot_player1_1,
R.drawable.jt5,
};
/**轮播图片的标题*/
private String[] titles=new String[]{
"我是一",
"我是二",
"我是三",
"我是四",
};
2、在你要轮播的布局里面加入下面的布局(相当于加入一个控件,看你想放哪里).
<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dip" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dip"
android:layout_gravity="bottom"
android:background="#33000000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片标题"
android:textColor="@android:color/white" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:orientation="horizontal" >
<View
android:id="@+id/dot_0"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_focused"/>
<View
android:id="@+id/dot_1"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_normal"/>
<View
android:id="@+id/dot_2"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_normal"/>
<View
android:id="@+id/dot_3"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_normal"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
3、把图片和标题都设置控件里面。
/**显示的图片*/
images = new ArrayList<ImageView>();
for(int i=0;i<imageIds.length;i++){
ImageView imageView = new ImageView(getActivity());
imageView.setBackgroundResource(imageIds[i]);
images.add(imageView);
}
/*显示的圆点 */
dots = new ArrayList<View>();
dots.add(view.findViewById(R.id.dot_0));
dots.add(view.findViewById(R.id.dot_1));
dots.add(view.findViewById(R.id.dot_2));
dots.add(view.findViewById(R.id.dot_3));
/**轮播的标题*/
title = (TextView) view.findViewById(R.id.title);
title.setText(titles[0]);
4、findViewById到控件布局里面的ViewPager,new 一个ViewpagerAdapter(),通过setOnPageChangeListener的方法来监听改变
viewPager = (ViewPager) view.findViewById(R.id.viewPager);
adapter = new ViewPagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
title.setText(titles[position]);
dots.get(position).setBackgroundResource(R.drawable.dot_focused);
dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);
oldPosition = position;
currentItem = position;
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
5、自定义一个ViewPagerAdapter
/**
* 自定义Adapter
* 内部类
*/
private class ViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return images.size();//传入的数据
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup view, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
// view.removeView(view.getChildAt(position));
// view.removeViewAt(position);
view.removeView(images.get(position));
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
// TODO Auto-generated method stub
view.addView(images.get(position));
return images.get(position);
}
}
6、这些自己看着改主要是线程池,handler,定时轮换
/**
* 图片轮播任务
*
*/
private class ViewPageTask implements Runnable{
@Override
public void run() {
currentItem = (currentItem + 1) % imageIds.length;
mHandler.sendEmptyMessage(0);
}
}
/**
* 接收子线程传递过来的数据
*/
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
viewPager.setCurrentItem(currentItem);
};
};
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(scheduledExecutorService != null){
scheduledExecutorService.shutdown();
scheduledExecutorService = null;
}
}
关于android中怎么实现一个自动轮播图效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。