一:闪屏页
闪屏页其实就是将闪屏页作为第一个启动的Activity,一般为图片,我们可以在这个 Activity中加入网络请求,请求图片,先为其设置默认图片,后请求图片将其替换,请求成功后保存至缓存一般闪屏页都有动画,在我们设置动画时需要注意,要当动画执行完成后再实现页面的跳转,这时就需要给改布局设置一个动画监听了,具体代码如下:
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
boolean is_firstenter = PrefUtils.getBoolean(SplashActivity.this, ConstantValues.IS_FIRSTENTER,true);
Intent intent =null;
if(is_firstenter){
intent =new Intent(SplashActivity.this,GuideActivity.class);
}else{
intent =new Intent(SplashActivity.this,MainActivity.class);
}
startActivity(intent);
finish();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
当动画执行完成时执行跳转
二: 新手引导页
效果如图:


这里我们采用的布局为ViewPager,其中指示器我们是采用向LinearLayout中加入圆点,最后的立即提示按钮是本身就有,向将其隐藏起来,当滑动到最后一个页面的时候,让其显示,并为其设置监听,实现跳转
布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GuideActivity">
android:id="@+id/vp_guide"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="65dp"
android:text="立即体验"
android:textColor="@color/selector_guard_txt"
android:background="@drawable/selector_guard_btn"
android:visibility="invisible"
android:padding="5dp"
android:textSize="30sp"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:layout_alignParentBottom="true">
<LinearLayout
android:id="@+id/ll_guide"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/iv_dot_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shape_point_red"
/>
</RelativeLayout>
</RelativeLayout>
步骤如下:
1.为viewpager设置滑动监听,在onPageScrolled中计算小红点的margin的大小,
vp_guide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//mPointDis为一个成员变量,他的值为viewpager的宽度,设置视图树的观察者,当生成这个视图时,即可
//得到几个小圆点的宽度,positionOffset在viewpager上滑动的百分比,leftmargin等于百分比乘以他们之间
// 的间距,即可使小红点跟随滑动
int leftmargin = (int) (mPointDis * positionOffset) + position *mPointDis;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)iv_dot_red.getLayoutParams();
params.leftMargin = leftmargin;
iv_dot_red.setLayoutParams(params);
}
@Override
public void onPageSelected(int position) {
if(position ==list.size() -1){
btn_guide.setVisibility(View.VISIBLE);
}else {
btn_guide.setVisibility(View.INVISIBLE);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
iv_dot_red.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
iv_dot_red.getViewTreeObserver().removeOnGlobalLayoutListener(this);
mPointDis =ll_guide.getChildAt(1).getLeft() -ll_guide.getChildAt(0).getLeft();
}
});
2.初始化数据:
根据网络请求下来图片的个数来生成ImageView,用集合来存储,并同时生成小圆点的图像,加到指示器的容器中去,
private void initData() {
for(int i=0;i
//初始化图片
ImageView imageView =new ImageView(this);
imageView.setBackgroundResource(picture[i]);
list.add(imageView);
LinearLayout.LayoutParams params =new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// 初始化小圆点
if(i >0){
params.leftMargin =15;
}
ImageView dotview =new ImageView(this);
dotview.setImageResource(R.drawable.shape_point_gray);
ll_guide.addView(dotview,params);
}
}
3.为viewpager设置适配器,
vp_guide.setAdapter(new GuideAdapter());
class GuideAdapterextends PagerAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@NonNull
@Override
public ObjectinstantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView =list.get(position);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}
}
网友评论