实习的第一个经理布置的任务就是实现引导页功能,在这和大家分享下
1.布局采用相对布局中有ViewPager和LinearLayout 代码如下:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vp_splash">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/point_group"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
</LinearLayout>
2.小圆点的定义如下:
一、这是小圆点的背影
<item android:drawable="@drawable/point_enable" android:state_enabled="true"></item>
<item android:drawable="@drawable/point_normal" android:state_enabled="false"></item>
二、获取到小圆点的焦点状态:
<solid android:color="#aaffffff" />
三、小圆点没有获取带哦焦点的状态
<solid android:color="#55000000" />
3.java中的实现代码如下
public class SplashActivity extends Activity {
private ViewPager mpager;
private ArrayList imageViewList;
private LinearLayout llPointGroup;
private int previousPosition = 0;//图片的索引
private ViewPagerAdapter adapter;
private GestureDetector gestureDetector; // 用户滑动
private int flaggingWidth;// 互动翻页所需滚动的长度是当前屏幕宽度的1/3
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
gestureDetector = new GestureDetector(new GuideViewTouch());
// 获取分辨率
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
flaggingWidth = dm.widthPixels / 3;
init();
}
private void init() {
mpager = (ViewPager) findViewById(R.id.vp_splash);
llPointGroup = (LinearLayout) findViewById(R.id.point_group);
//图片资源
int[] imageResourseIDs = new int[]{R.drawable.splash1, R.drawable.splash2, R.drawable.splash3};
imageViewList = new ArrayList<ImageView>();
ImageView iv;
// 小圆点view
View view;
for (int i = 0; i < imageResourseIDs.length; i++) {
iv = new ImageView(this);
iv.setBackgroundResource(imageResourseIDs[i]);
imageViewList.add(iv);
// 添加点view对象
view = new View(this);
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.point_background));
RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(20, 20);
lp.leftMargin = 20;
view.setLayoutParams(lp);
view.setEnabled(false);
llPointGroup.addView(view);
}
adapter = new ViewPagerAdapter();
mpager.setAdapter(adapter);
mpager.setCurrentItem(previousPosition);
llPointGroup.getChildAt(previousPosition).setEnabled(true);
mpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageSelected(int position) {
llPointGroup.getChildAt(position % imageViewList.size()).setEnabled(
true);
llPointGroup.getChildAt(previousPosition).setEnabled(false);
previousPosition = position % imageViewList.size();
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
return super.dispatchTouchEvent(event);
}
private class GuideViewTouch extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (previousPosition == imageViewList.size() - 1) {
if (Math.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY()
- e2.getY())
&& (e1.getX() - e2.getX() <= (-flaggingWidth) || e1
.getX() - e2.getX() >= flaggingWidth)) {
if (e1.getX() - e2.getX() >= flaggingWidth) {
GoToMainActivity();
return true;
}
}
}
return false;
}
}
private void GoToMainActivity() {
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}
//适配器
private class ViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return imageViewList.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(imageViewList.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(imageViewList.get(position));
return imageViewList.get(position);
}
}
}