1.布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.wxlb.MainActivity"> <android.support.v4.view.ViewPager android:layout_width="386dp" android:layout_height="300dp" android:id="@+id/vp" ></android.support.v4.view.ViewPager> <LinearLayout android:orientation="horizontal" android:id="@+id/li" android:layout_width="match_parent" android:layout_height="20dp"></LinearLayout> </RelativeLayout>2.drawable里放入图片
3.Activity
package com.example.wxlb; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity { int index; private LinearLayout li; private ViewPager vp; int ids[]=new int[]{R.drawable.r,R.drawable.tt,R.drawable.w,R.drawable.x}; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); index++; while (index>=5){ index=0; } vp.setCurrentItem(index); handler.sendEmptyMessageDelayed(0,200); } }; private MyBase myBase; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp = (ViewPager) findViewById(R.id.vp); li = (LinearLayout) findViewById(R.id.li); myBase = new MyBase(); vp.setAdapter(myBase); inticot(); vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { for (int i=0;i<li.getChildCount();i++) { ImageView img =(ImageView) li.getChildAt(i); if (i==position%ids.length) { img.setImageResource(R.drawable.point_selected); }else { img.setImageResource(R.drawable.point_mormal); } } } @Override public void onPageScrollStateChanged(int state) { } }); handler.sendEmptyMessageDelayed(0,2000); } private void inticot() { for (int i=0;i<ids.length;i++) { ImageView imageView = new ImageView(MainActivity.this); if (i==0){ imageView.setImageResource(R.drawable.point_selected); } else { imageView.setImageResource(R.drawable.point_mormal); } LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(10, 10); layoutParams.setMargins(10,5,10,5); li.addView(imageView,layoutParams); } } class MyBase extends PagerAdapter{ @Override public int getCount() { return ids.length; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(MainActivity.this); imageView.setImageResource(ids[position]); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View)object); } } }4.一定在清单文件中加入联网操作