闪屏页的实现 :
import android.app.Activity;
import android.content.Intent;
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;
import com.example.pc.zhbj52.utils.PrefUtils;
/**
* 闪屏页
*
* @author Kevin
*
*/
public class SplashActivity extends Activity {
RelativeLayout rlRoot;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
rlRoot = (RelativeLayout) findViewById(R.id.rl_root);
startAnim();
//LibUtils.doSomething();
//rlRoot.setBackgroundResource(R.drawable.newscenter_press);
}
/**
* 开启动画
*/
private void startAnim() {
// 动画集合
AnimationSet set = new AnimationSet(false);
// 旋转动画
RotateAnimation rotate = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(1000);// 动画时间
rotate.setFillAfter(true);// 保持动画状态
// 缩放动画
ScaleAnimation scale = new ScaleAnimation(0, 1, 0, 1,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
scale.setDuration(1000);// 动画时间
scale.setFillAfter(true);// 保持动画状态
// 渐变动画
AlphaAnimation alpha = new AlphaAnimation(0, 1);
alpha.setDuration(2000);// 动画时间
alpha.setFillAfter(true);// 保持动画状态
set.addAnimation(rotate);
set.addAnimation(scale);
set.addAnimation(alpha);
// 设置动画监听
set.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
// 动画执行结束
@Override
public void onAnimationEnd(Animation animation) {
jumpNextPage();
}
});
rlRoot.startAnimation(set);
}
/**
* 跳转下一个页面
*/
private void jumpNextPage() {
// 判断之前有没有显示过新手引导
boolean userGuide = PrefUtils.getBoolean(this, "is_user_guide_showed",
false);
if (!userGuide) {
// 跳转到新手引导页
startActivity(new Intent(SplashActivity.this, GuideActivity.class));
} else {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
}
finish();
}
}
工具类
import android.content.Context;
import android.content.SharedPreferences;
/**
* SharePreference封装
*
* @author Kevin
*
*/
public class PrefUtils {
public static final String PREF_NAME = "config";
public static boolean getBoolean(Context ctx, String key,
boolean defaultValue) {
SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
Context.MODE_PRIVATE);
return sp.getBoolean(key, defaultValue);
}
public static void setBoolean(Context ctx, String key, boolean value) {
SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
Context.MODE_PRIVATE);
sp.edit().putBoolean(key, value).commit();
}
}
在新手引导页的写法是
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 更新sp, 表示已经展示了新手引导
PrefUtils.setBoolean(GuideActivity.this,
"is_user_guide_showed", true);
在主页面不需要再做判断 ,因为闪屏页已经判断了。
另外一种类似的写法,但是比较全
欢迎页
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
public class WelcomeAct extends Activity {
private static final int TIME = 2000;
private static final int GO_HOME = 1000;
private static final int GO_GUIDE = 1001;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case GO_HOME:
goHome();
break;
case GO_GUIDE:
goGuide();
break;
}
}
;
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
init();
}
private void init() {
// SharedPreferences perPreferences = getSharedPreferences("jike", MODE_PRIVATE);
//
app第一次打开,获取的值“isFristIn”默认的值是true
// isFirstIn = perPreferences.getBoolean("isFirstIn", true);
!isFristIn意思是!true
boolean userGuide = PrefUtils.getBoolean(this, "isFirstIn",
false);
if (userGuide) {
//进入主页,进入的时候还有一个时间的延时
mHandler.sendEmptyMessageDelayed(GO_HOME, TIME);
} else {
//进入引导页 进入的时候还有一个时间的延时
mHandler.sendEmptyMessageDelayed(GO_GUIDE, TIME);
//储存值“isFristIn”
// Editor editor = perPreferences.edit();
// editor.putBoolean("isFirstIn", false);
// editor.commit();
}
}
private void goHome() {
Intent i = new Intent(WelcomeAct.this, MainActivity.class);
startActivity(i);
finish();
}
private void goGuide() {
Intent i = new Intent(WelcomeAct.this, Guide.class);
startActivity(i);
finish();
}
}
新手引导页
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class Guide extends Activity implements OnPageChangeListener {
private ViewPager vp;
private ViewPagerAdapter vpAdapter;
private List<View> views;
private ImageView[] dots;
private int[] ids = {R.id.iv1, R.id.iv2, R.id.iv3};
private Button start_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide);
initViews();
initDots();
}
private void initViews() {
LayoutInflater inflater = LayoutInflater.from(this);
views = new ArrayList<View>();
views.add(inflater.inflate(R.layout.one, null));
views.add(inflater.inflate(R.layout.two, null));
views.add(inflater.inflate(R.layout.three, null));
//数据源
vpAdapter = new ViewPagerAdapter(views, this);
//列表视图
vp = (ViewPager) findViewById(R.id.viewpager);
//填充到ui activity中
vp.setAdapter(vpAdapter);
//第二个(第三幅图片"three")的views的button
start_btn = (Button) views.get(2).findViewById(R.id.start_btn);
start_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//表示已经展示引导页
PrefUtils.setBoolean(Guide.this,"isFirstIn",true);
Intent i = new Intent(Guide.this, MainActivity.class);
startActivity(i);
finish();
}
});
//这是一个回调方法
vp.setOnPageChangeListener(this);
}
//找id
private void initDots() {
dots = new ImageView[views.size()];
for (int i = 0; i < views.size(); i++) {
dots[i] = (ImageView) findViewById(ids[i]);
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
//这个是点进行改变的关键代码(页面被选中的状态)
//只要你手指在滑动,就会触动这个方法里面的for循环语句,它会进行三次循环i=0,1,2然后跳出循环,每滑动一个页面就会调用一次
@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < ids.length; i++) {
//如果agr0的值等于i,arg0的值分别是0,1,2,i的值表示的是R.id.v1 v2 v3对应的三个点的id,就是对应的点
//加在一起的意思是就是 页面对应相对应的点的id
if (arg0 == i) {
dots[i].setImageResource(R.drawable.login_point_selected);
} else {
dots[i].setImageResource(R.drawable.login_point);
}
}
}
}
适配器
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import java.util.List;
public class ViewPagerAdapter extends PagerAdapter {
private List<View> views;
private Context context;
public ViewPagerAdapter(List<View> views, Context context) {
this.views = views;
this.context = context;
}
@Override
public void destroyItem(View container, int position, Object object) {
//销毁views
((ViewPager) container).removeView(views.get(position));
}
@Override
public Object instantiateItem(View container, int position) {
((ViewPager) container).addView(views.get(position));
return views.get(position);
}
@Override
public int getCount() {
return views.size();
}
//判断当前的view是不是需要的对象
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return (arg0 == arg1);
}
}
主页
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
工具类
import android.content.Context;
import android.content.SharedPreferences;
/**
* Created by pc on 2016/4/26.
*/
public class PrefUtils {
public static final String PREF_NAME = "jike";
//需要添加Context,这样就知道是哪个对象来调用getSharedPreferenced
public static boolean getBoolean(Context ctx, String key,
boolean defaultValue) {
SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
Context.MODE_PRIVATE);
return sp.getBoolean(key, defaultValue);
}
public static void setBoolean(Context ctx,String key, boolean value) {
SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
Context.MODE_PRIVATE);
sp.edit().putBoolean(key, value).commit();
}
}
源码地址: http://pan.baidu.com/s/1pLjBAMB