首先应该添加viewpage的监听
其次判断原点的距离
再次通过Viewpage的移动距离来移动小圆点
上code
设置覆盖点
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#f00" />
</shape>
关于GuideActivity
package com.example.admin.zhbj;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.util.ArrayList;
public class GuideActivity extends Activity {
private ViewPager vpGuide;
private Button btnStart;
ArrayList<ImageView> mImageViewList;
private LinearLayout llPointGroup;//引导圆点的父控件
private int mPointWidth;//原点间的距离
private View viewRedPoint;
private static final int[] ImageIds = new int[]{R.mipmap.guide_1, R.mipmap.guide_2, R.mipmap.guide_3};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
setContentView(R.layout.activity_guide);
// Full Screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
llPointGroup = (LinearLayout) findViewById(R.id.ll_point_group);//引导原点父控件
vpGuide = (ViewPager) findViewById(R.id.vp_guide);
btnStart = (Button) findViewById(R.id.btn_start);
viewRedPoint = findViewById(R.id.view_red_point);
initView();
vpGuide.setAdapter(new MyViewPageAdpater());
vpGuide.addOnPageChangeListener(new GuidepageListener());
}
protected void initView() {
mImageViewList = new ArrayList<ImageView>();
//初始化引导页的3个页面
for (int i = 0; i < ImageIds.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(ImageIds[i]);
mImageViewList.add(imageView);
}
// 初始化页面的小圆点
for (int i = 0; i < ImageIds.length; i++) {
View point = new View(this);
point.setBackgroundResource(R.drawable.shap_point_gray);//设置引导页默认原点
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
if (i > 0) {
params.leftMargin = 20;//设置点间隔
}
point.setLayoutParams(params);
llPointGroup.addView(point);//将原点添加给线性布局
//获取视图树
llPointGroup.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//当layout结束时会执行此方法
@Override
public void onGlobalLayout() {
System.out.println("layout 结束");
llPointGroup.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//基础三部曲
//measure(测量布局大小) layout(界面的位置) omdraw(绘制)
mPointWidth = llPointGroup.getChildAt(1).getLeft() - llPointGroup.getChildAt(0).getLeft();
}
});
}
}
/**
* viewpage 的适配器
*/
class MyViewPageAdpater extends PagerAdapter {
@Override
public int getCount() {
return ImageIds.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(mImageViewList.get(position));
return mImageViewList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
/**
* viewPage 的滑动监听
*/
class GuidepageListener implements ViewPager.OnPageChangeListener {
//滑动的事件
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//当前位置:position,百分比:positionOffset,移动距离:positionOffsetPixels
//小圆点移动的宽度
int len= (int) (mPointWidth*positionOffset + mPointWidth*position);
//获取当前红点点的布局参数
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewRedPoint.getLayoutParams();
params.leftMargin = len;
//再次设置小红点的布局参数
viewRedPoint.setLayoutParams(params);
}
//某个页面被选中
@Override
public void onPageSelected(int position) {
}
//滑动状态发生变化
@Override
public void onPageScrollStateChanged(int state) {
}
}
}
xml布局
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.admin.zhbj.GuideActivity">
<android.support.v4.view.ViewPager
android:id="@+id/vp_guide"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="55dp"
android:background="@drawable/btn_guide_selector"
android:gravity="center"
android:padding="5dp"
android:text="开启体验之旅"
android:textColor="@drawable/btn_textguide_selector"
android:textSize="18dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp">
<LinearLayout
android:id="@+id/ll_point_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
</LinearLayout>
<View
android:id="@+id/view_red_point"
android:layout_width="5dp"
android:layout_height="5dp"
android:background="@drawable/shape_point_red"
/>
</RelativeLayout>
</RelativeLayout>
效果图:
传承者(Inheritors)欢迎各位纠正错误,评论,吐槽!!!