大神略过,如有不足,请轻喷!!!最后附上下载地址。
在开始本篇文章前,咱们先来聊聊实现的思路。引导界面,ViewPager这是毋庸置疑的。然后就是圆点的实现,咱们可以自己画出来相应的圆点以及相邻圆点之间的距离。利用ViewPager图片移动百分比(屏幕宽度)动态算出圆点要移动的距离。
ViewPager的实现相信来到这篇文章的童鞋都是会的,就直接贴代码了
@Override
public int getCount() {
return arrayImages.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView iv = arrayImages.get(position);
container.addView(iv);
return iv;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
接下来的就是圆点的实现了:
- 首先需要在布局上画出咱们的圆点个数:
// 动态得出圆点的个数,距离,并添加到线性布局中
for (int i = 0;i<mImages.length;i++){
ImageView iv = new ImageView(this);
iv.setBackgroundResource(mImages[i]);
mImageViewArrayList.add(iv);
ImageView circleView = new ImageView(this);
circleView.setBackgroundResource(R.drawable.guide_circle_shape_select);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
if (i>0){
params.leftMargin = 8;
}
circleView.setLayoutParams(params);
mLlContainear.addView(circleView);//在添加到布局文件中是的时需要注意一点:要添加的父布局是什么布局,这里添加的就是什么布局。
}
- 得到两个点之间的距离:(必须要页面绘制完成,否者得到的距离为0)
mGuide_red_point.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mGuide_red_point.getViewTreeObserver().removeOnGlobalLayoutListener(this);
mPointWidth = mLlContainear.getChildAt(1).getLeft()-
mLlContainear.getChildAt(0).getLeft();
}
});
- 利用百分比得到移动的距离并赋值:
@Override
public void onPageScrolled(int position,
float positionOffset, int positionOffsetPixels) {
int leftMargin = (int) (position * mPointWidth + mPointWidth * positionOffset);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
mGuide_red_point.getLayoutParams();
params.leftMargin = leftMargin;
mGuide_red_point.setLayoutParams(params);
}
最后附上完整的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
mGuide_viewpager = (ViewPager) findViewById(R.id.guide_viewpager);
mGuide_begin = (Button) findViewById(R.id.guide_begin);
mLlContainear = (LinearLayout)findViewById(R.id.llContainer);
mGuide_red_point = (ImageView)findViewById(R.id.guide_red_point);
mImages = new int[]{R.drawable.guide_1,
R.drawable.guide_3,
R.drawable.guide_4,
R.drawable.guide_5};
mImageViewArrayList = new ArrayList<ImageView>();
// 动态得出圆点的个数,距离,并添加到线性布局中
for (int i = 0;i<mImages.length;i++){
ImageView iv = new ImageView(this);
iv.setBackgroundResource(mImages[i]);
mImageViewArrayList.add(iv);
ImageView circleView = new ImageView(this);
circleView.setBackgroundResource(R.drawable.guide_circle_shape_select);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
if (i>0){
params.leftMargin = 8;
}
circleView.setLayoutParams(params);
mLlContainear.addView(circleView);
}
mGuide_red_point.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mGuide_red_point.getViewTreeObserver().removeOnGlobalLayoutListener(this);
mPointWidth = mLlContainear.getChildAt(1).getLeft()-
mLlContainear.getChildAt(0).getLeft();
}
});
Guide_Adapter ga = new Guide_Adapter(mImageViewArrayList,this);
mGuide_viewpager.setAdapter(ga);
mGuide_viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position,
float positionOffset, int positionOffsetPixels) {
int leftMargin = (int) (position * mPointWidth + mPointWidth * positionOffset);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
mGuide_red_point.getLayoutParams();
params.leftMargin = leftMargin;
mGuide_red_point.setLayoutParams(params);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}