广告轮播图
轮播图使用ViewPager来实现,我没有抽取为view,直接在activity中实现
public class MainActivity extends Activity implements OnPageChangeListener {
//创建数组存储图片ID
private int[] img={R.drawable.icon_1,R.drawable.icon_2,R.drawable.icon_3,R.drawable.icon_4,R.drawable.icon_5};
//图片标题
private String[] str={"为梦想坚持", "我相信我", "为公开课而生", "Google IO", "轻松1w+"};
private ViewPager mViewpager;
private TextView mText;
private LinearLayout mContainer;
private int mLastPosition=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initListener();
}
//获取控件对象
public void initView() {
mViewpager = (ViewPager) findViewById(R.id.view_pager);
mText = (TextView) findViewById(R.id.text);
mContainer = (LinearLayout) findViewById(R.id.dots_container);
}
public void initData() {
mViewpager.setAdapter(new MyAdapter());
//计算第一次出现的图
int position=Integer.MAX_VALUE/2-(Integer.MAX_VALUE/2%img.length);
//设置第一次出现的位置
mViewpager.setCurrentItem(position);
mText.setText(str[0]);
//动态的添加点
for (int i = 0; i < img.length; i++) {
View view=new View(this);
//将dp变成像素
int size = getResources().getDimensionPixelSize(R.dimen.size);
LinearLayout.LayoutParams layout=new LinearLayout.LayoutParams(size, size);
if(i!=img.length-1){
layout.rightMargin=size;
}
view.setLayoutParams(layout);
if(i==0){
view.setBackgroundResource(R.drawable.bg_dot_selected);
}else{
view.setBackgroundResource(R.drawable.bg_dot_normal);
}
mContainer.addView(view);
}
}
public void initListener() {
mViewpager.setOnPageChangeListener(this);
}
public class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
//初始化一个界面
@Override
public Object instantiateItem(ViewGroup container, int position) {
int num=position%img.length;
ImageView view=new ImageView(getApplicationContext());
view.setImageResource(img[num]);
mViewpager.addView(view);
return view;
}
//销毁一个界面
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
mViewpager.removeView((View) object);
}
}
//页面滚动状态的变化
@Override
public void onPageScrollStateChanged(int arg0) {
}
//页面滚动的时候调用
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
//页面被选中的时候调用
@Override
public void onPageSelected(int position) {
position = position % img.length;
int num=position%str.length;
mText.setText(str[num]);
if(mLastPosition==position){
return;
}
//更新点
View view = mContainer.getChildAt(position);
view.setBackgroundResource(R.drawable.bg_dot_selected);
//获取上次的点
View view2 = mContainer.getChildAt(mLastPosition);
view2.setBackgroundResource(R.drawable.bg_dot_normal);
mLastPosition=position;
}
}
-------------------------------------------------------------------
布局
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="180dp"
tools:context=".MainActivity" >
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#33999999"
android:orientation="vertical" >
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="5dp"
android:text="哈哈"
android:textColor="#ffffff"
android:textSize="18sp" />
android:id="@+id/dots_container"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
广告轮播图