ViewPager—01引导页的制作
布局文件
<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/vp" android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" > <LinearLayout android:id="@+id/ll_points" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > </LinearLayout> <View android:id="@+id/view_white" android:layout_width="10dp" android:layout_height="10dp" android:background="@drawable/white_point" /> </RelativeLayout> </RelativeLayout>
相关代码
1 public class MainActivity extends Activity { 2 3 private ViewPager mViewPager; 4 private int[] ids = {R.drawable.a1,R.drawable.a2,R.drawable.a3}; 5 private List<ImageView> mImageViews; 6 private MyPagerAdapter mPagerAdapter; 7 private LinearLayout mLlPoints; 8 private View mViewPoint; 9 10 private int dis_left; //白点距离左边的距离 11 private int dis_pp; //两个点之间的距离 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 requestWindowFeature(Window.FEATURE_NO_TITLE); 17 setContentView(R.layout.activity_main); 18 19 initView(); 20 initData(); 21 22 mViewPoint.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 23 24 @Override 25 public void onGlobalLayout() { 26 mViewPoint.getViewTreeObserver().removeGlobalOnLayoutListener(this); 27 dis_pp = mLlPoints.getChildAt(1).getLeft() - mLlPoints.getChildAt(0).getLeft(); 28 } 29 }); 30 31 mPagerAdapter = new MyPagerAdapter(); 32 mViewPager.setAdapter(mPagerAdapter); 33 34 mViewPager.addOnPageChangeListener(new OnPageChangeListener() { 35 @Override 36 public void onPageSelected(int arg0) { 37 38 } 39 40 @Override 41 public void onPageScrolled(int arg0,float arg1,int arg2) { 42 //设置白点的移动距离 43 dis_left = (int)((arg0 + arg1) * dis_pp); 44 RelativeLayout.LayoutParams params = (LayoutParams)mViewPoint.getLayoutParams(); 45 params.leftMargin = dis_left; 46 mViewPoint.setLayoutParams(params); 47 } 48 49 @Override 50 public void onPageScrollStateChanged(int arg0) { 51 52 } 53 }); 54 55 56 } 57 58 private void initView() { 59 mViewPager = (ViewPager)findViewById(R.id.vp); 60 mLlPoints = (LinearLayout)findViewById(R.id.ll_points); 61 mViewPoint = findViewById(R.id.view_white); 62 } 63 64 private void initData() { 65 66 mImageViews = new ArrayList<ImageView>(); 67 68 for (int i = 0;i < ids.length;i++) { 69 70 ImageView iv = new ImageView(this); 71 iv.setBackgroundResource(ids[i]); 72 mImageViews.add(iv); 73 74 View view = new View(this); 75 view.setBackgroundResource(R.drawable.gray_point); 76 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Utils.dip2px(this,10), 77 Utils.dip2px(this,10)); 78 if(i != 0){ 79 params.leftMargin = Utils.dip2px(this,20); 80 } 81 view.setLayoutParams(params); 82 83 mLlPoints.addView(view); 84 } 85 } 86 87 class MyPagerAdapter extends PagerAdapter { 88 89 @Override 90 public int getCount() { 91 return mImageViews.size(); 92 } 93 94 @Override 95 public Object instantiateItem(ViewGroup container,int position) { 96 ImageView iv = mImageViews.get(position); 97 container.addView(iv); 98 return iv; 99 } 100 101 @Override 102 public boolean isViewFromObject(View arg0,Object arg1) { 103 return arg0 == arg1; 104 } 105 106 @Override 107 public void destroyItem(ViewGroup container,int position,Object object) { 108 container.removeView((View)object); 109 } 110 111 } 112 }
效果图
越努力,越幸运!