实现比较简单,封装sharedPreferences如下,方便后面使用sharedPreferences。
com.ldw.news.utils/PreferencesUtils.java
package com.ldw.news.Utils;
import android.content.Context;
import android.content.SharedPreferences;
/**
* Created by ${ldw} on 2017/7/5.
*/
public class PreferencesUtils {
public static final String PREF_NAME = "config";
//获取到SharedPreference中的数据
public static boolean getBoolean(Context context, String key, boolean defaultBoolean){
SharedPreferences sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
return sp.getBoolean(key,defaultBoolean);
}
//往SharedPreferences保存数据
public static void putBoolean(Context context, String key, boolean value){
SharedPreferences sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
sp.edit().putBoolean(key, value).commit();
}
}
闪频页面读取sharedPreferences的值来判断是不是需要进入导航页还是直接进入主页
SplashActivity.java
package com.ldw.news;
import com.ldw.news.utils.PreferencesUtils;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
public class SplashActivity extends Activity {
private RelativeLayout rl_root;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//获取到splash页面的总布局
rl_root = (RelativeLayout) findViewById(R.id.rl_root);
//spalsh进入图片的旋转动画
startAnimation();
}
/*
* spalsh的动画
*/
private void startAnimation(){
//创建一个AnimationSet来支持多种动画特效
AnimationSet set = new AnimationSet(false);
//前两个参数是旋转的角度,后四个参数是相对于自己的中心
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//动画维持时间
rotateAnimation.setDuration(3000);
//保持最后的状态
rotateAnimation.setFillAfter(true);
//旋转的过程中大小会变化
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//动画维持时间
scaleAnimation.setDuration(3000);
//保持最后的状态
scaleAnimation.setFillAfter(true);
//参数为透明值由不透明到完全透明
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
//动画维持时间
alphaAnimation.setDuration(3000);
//保持最后的状态
alphaAnimation.setFillAfter(true);
//set集合中添加多个动画特效
set.addAnimation(scaleAnimation);
set.addAnimation(rotateAnimation);
set.addAnimation(alphaAnimation);
//设置动画监听,动画结束以后跳转aactivity
set.setAnimationListener(new AnimationListener(){
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
//动画执行结束
@Override
public void onAnimationEnd(Animation animation) {
//页面的跳转
jumpNextPage();
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
});
//执行动画
rl_root.startAnimation(set);
}
/*
* 页面的跳转
*/
private void jumpNextPage(){
/*使用封装实现
//判断是不是第一次进入引导页
sp = getSharedPreferences("config", MODE_PRIVATE);
boolean userGuide = sp.getBoolean("is_first_userGuide", false);
*/
boolean userGuide = PreferencesUtils.getBoolean(this, "is_first_userGuide", false);
//第一次进入的时候显示引导页
if(!userGuide){
//跳转到引导页
startActivity(new Intent(SplashActivity.this, GuideActivity.class));
}else{
startActivity(new Intent(SplashActivity.this, MainActivity.class));
}
finish();
}
}
第一次进入导航页以后需要更新sharedPreferences保存的值,避免下次再进入,这个里面在布局中设置导航页的按钮点击为VISIBLE来控制页面按钮的显示与否,只在最后一个页面才显示可以点击的按钮
GuideActivity.java
package com.ldw.news;
import java.util.ArrayList;
import com.ldw.news.utils.PreferencesUtils;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
/*
* 引导页
*/
public class GuideActivity extends Activity {
private ViewPager vp_guide;
private Button btn_start;//开始体验按钮
private View view_red_point;
private LinearLayout ll_point;//引导页下面的点
private int mPointWidth;//两个圆点之间的距离
private static final int[] mImageIds = new int[] { R.drawable.guide_1,
R.drawable.guide_2, R.drawable.guide_3 };//初始化三张图片
private ArrayList<ImageView> mImageList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化界面
initView();
//填充内容
vp_guide.setAdapter(new GuideAdapter());
//监听viewPager的滑动时间
vp_guide.setOnPageChangeListener(new GuidePageListener());
}
/*
* 初始化界面
*/
private void initView(){
requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题
mImageList = new ArrayList<ImageView>();
setContentView(R.layout.activity_guide);
vp_guide = (ViewPager) findViewById(R.id.vp_guide);
btn_start = (Button) findViewById(R.id.btn_start);
ll_point = (LinearLayout) findViewById(R.id.ll_point);
view_red_point = findViewById(R.id.view_red_point);
btn_start.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
/*使用封装实现
//更新sp,避免再次进入引导页
SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
//更新sp
sp.edit().putBoolean("is_first_userGuide", true).commit();
*/
PreferencesUtils.setBoolean(GuideActivity.this, "is_first_userGuide", true);
//跳转到主页
startActivity(new Intent(GuideActivity.this, MainActivity.class));
finish();
}
});
//添加图片,初始化引导页
for(int i = 0; i < mImageIds.length; i++){
ImageView image = new ImageView(this);
//设置引导页的背景
image.setBackgroundResource(mImageIds[i]);
mImageList.add(image);
}
//初始化引导页下方的小圆点
for(int i = 0; i < mImageIds.length; i++){
View point = new View(this);
//设置引导页的背景
point.setBackgroundResource(R.drawable.shape_point_gray);
//创建一个线性布局参数的大小是10,10
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
if(i > 0){
//设置圆点之间的间隔
params.leftMargin = 10;
}
//设置圆点的大小
point.setLayoutParams(params);
//将圆点添加到线性布局中
ll_point.addView(point);
}
//measure测量大小,layout界面布局,ondraw画图
//view的视图树getViewTreeObserver方法,并添加监听全局layout结束事件,这个时候可以获取到距离了
ll_point.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
//layout执行结束以后回调此方法
@Override
public void onGlobalLayout() {
//第二哥圆点距离左边的距离和第一个点距离左边距离的差是移动的距离
mPointWidth = ll_point.getChildAt(1).getLeft() - ll_point.getChildAt(0).getLeft();
//回调方法结束以后去掉监听
ll_point.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
/*
* ViewPager页面适配
*/
class GuideAdapter extends PagerAdapter{
@Override
public int getCount() {
return mImageIds.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
//初始化界面
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(mImageList.get(position));
return mImageList.get(position);
}
//删除条目
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
//viewPager的监听
class GuidePageListener implements OnPageChangeListener{
//滑动事件
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
System.out.println("当前位置:" +position + ";百分比:"+ positionOffset + ";移动距离:"+ positionOffsetPixels);
//圆点实时移动的距离,前部分是宽度的移动,后面是加上圆点的位置宽度
int length = (int) (mPointWidth * positionOffset) + position * mPointWidth;
//继承父类RelativeLayout获取到红点布局参数,红点是在布局中创建了因此不需要创建LayoutParams
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view_red_point.getLayoutParams();
//动态修改红点的左边的边距
layoutParams.leftMargin = length;
//红点重新设置边距参数,实现移动
view_red_point.setLayoutParams(layoutParams);
}
//页面被选中
@Override
public void onPageSelected(int position) {
//最后一个页面被选中的时候,按钮出现,其他的时候不出现
if(position == mImageIds.length - 1){
btn_start.setVisibility(View.VISIBLE);
}else{
btn_start.setVisibility(View.INVISIBLE);
}
}
//滑动状态发生变化
@Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
}
}