布局文件
<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>
相关代码
public class MainActivity extends Activity { private ViewPager mViewPager;
private int[] ids = {R.drawable.a1,R.drawable.a2,R.drawable.a3};
private List<ImageView> mImageViews;
private MyPagerAdapter mPagerAdapter;
private LinearLayout mLlPoints;
private View mViewPoint; private int dis_left; //白点距离左边的距离
private int dis_pp; //两个点之间的距离 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); initView();
initData(); mViewPoint.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override
public void onGlobalLayout() {
mViewPoint.getViewTreeObserver().removeGlobalOnLayoutListener(this);
dis_pp = mLlPoints.getChildAt(1).getLeft() - mLlPoints.getChildAt(0).getLeft();
}
}); mPagerAdapter = new MyPagerAdapter();
mViewPager.setAdapter(mPagerAdapter); mViewPager.addOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) { } @Override
public void onPageScrolled(int arg0,float arg1,int arg2) {
//设置白点的移动距离
dis_left = (int)((arg0 + arg1) * dis_pp);
RelativeLayout.LayoutParams params = (LayoutParams)mViewPoint.getLayoutParams();
params.leftMargin = dis_left;
mViewPoint.setLayoutParams(params);
} @Override
public void onPageScrollStateChanged(int arg0) { }
}); } private void initView() {
mViewPager = (ViewPager)findViewById(R.id.vp);
mLlPoints = (LinearLayout)findViewById(R.id.ll_points);
mViewPoint = findViewById(R.id.view_white);
} private void initData() { mImageViews = new ArrayList<ImageView>(); for (int i = 0;i < ids.length;i++) { ImageView iv = new ImageView(this);
iv.setBackgroundResource(ids[i]);
mImageViews.add(iv); View view = new View(this);
view.setBackgroundResource(R.drawable.gray_point);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Utils.dip2px(this,10),
Utils.dip2px(this,10));
if(i != 0){
params.leftMargin = Utils.dip2px(this,20);
}
view.setLayoutParams(params); mLlPoints.addView(view);
}
} class MyPagerAdapter extends PagerAdapter { @Override
public int getCount() {
return mImageViews.size();
} @Override
public Object instantiateItem(ViewGroup container,int position) {
ImageView iv = mImageViews.get(position);
container.addView(iv);
return iv;
} @Override
public boolean isViewFromObject(View arg0,Object arg1) {
return arg0 == arg1;
} @Override
public void destroyItem(ViewGroup container,int position,Object object) {
container.removeView((View)object);
} }
}
效果图