Android项目实战(三):实现第一次进入软件的引导页

时间:2021-03-02 16:13:13

最近做的APP接近尾声了,就是些优化工作了,

我们都知道现在的APP都会有引导页,就是安装之后第一次打开才显示的引导页面(介绍这个软件的几张可以切换的图)

自己做了一下,结合之前学过的

慕课网_ViewPager切换动画(3.0版本以上有效果)

思路很简单,APP的主界面还是作为主Activity,只要新添加一个类来判断是不是第一次打开APP

设主activity 名字为:MainActivity.java   判断是不是第一次打开APP且实现引导页面的类 LoginActivity ,另外还需要一个类 这是安卓开发_慕课网_ViewPager切换动画(3.0版本以上有效果)中用到的 ZoomOutPageTransformer.java

1.在主activity中,在一开始 先跳转到LoginActivity类中

  protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
startActivity(new Intent(MainActivity.this, LoginActivity.class)); //跳转到LoginActivity类中
//初始化
init();
initEvent();
//默认显示第一个功能的界面(微信界面)
setSelect();
}

2.在LoginActivity类中判断是不是第一次打开,如果是,则显示ViewPager切换动画效果

如果不是,则执行finish();语句,直接结束这个Activity  自然的就回到了主Activity中了

LoginActivity代码

 package com.example.qunxiong;

 import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
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.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType; public class LoginActivity extends Activity {
private ViewPager mviewpager; //控件
private int[] mImgIds = new int[]{R.drawable.bg_rank_shenqi_0,R.drawable.bg_rank_shenqi_1,R.drawable.bg_rank_shenqi_10};//初始化图片
private List<ImageView> mImages = new ArrayList<ImageView>();
private Button jump;
private SharedPreferences preferences;
private Editor editor;
private OutputStream os;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
preferences = getSharedPreferences("phone", Context.MODE_PRIVATE);
jump = (Button) findViewById(R.id.jump);
jump.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
//判断是不是首次登录,
if (preferences.getBoolean("firststart", true)) {
editor = preferences.edit();
//将登录标志位设置为false,下次登录时不在显示首次登录界面
editor.putBoolean("firststart", false);
editor.commit();
mviewpager = (ViewPager) findViewById(R.id.id_viewpager);
//为ViewPage切换添加动画效果(3.0以上版本才可以有效果)
mviewpager.setPageTransformer(true, new ZoomOutPageTransformer()); mviewpager.setAdapter(new PagerAdapter() { @Override
public Object instantiateItem(View container, int position) {
ImageView imageview = new ImageView(LoginActivity.this);
imageview.setImageResource(mImgIds[position]);
imageview.setScaleType(ScaleType.CENTER_CROP); //设置图片不变形
((ViewGroup) container).addView(imageview);
mImages.add(imageview);
return imageview;
};
@Override
public void destroyItem(View container, int position, Object object) {
// TODO Auto-generated method stub ((ViewPager) container).removeView(mImages.get(position));
}
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == object;
} @Override
public int getCount() { //返回图片个数
// TODO Auto-generated method stub
return mImgIds.length;
}
});
}
else
{
finish();
} }
}

切换动画效果代码

ZoomOutPageTransformer.java
 package com.example.qunxiong;

 import android.annotation.SuppressLint;
import android.support.v4.view.ViewPager;
import android.view.View; public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.85f;
private static final float MIN_ALPHA = 0.5f; @SuppressLint("NewApi") public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight(); if (position < -) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(); } else if (position <= ) { // [-1,1]
// Modify the default slide transition to shrink the page as well
float scaleFactor = Math.max(MIN_SCALE, - Math.abs(position));
float vertMargin = pageHeight * ( - scaleFactor) / ;
float horzMargin = pageWidth * ( - scaleFactor) / ;
if (position < ) {
view.setTranslationX(horzMargin - vertMargin / );
} else {
view.setTranslationX(-horzMargin + vertMargin / );
} // Scale the page down (between MIN_SCALE and 1)
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor); // Fade the page relative to its size.
view.setAlpha(MIN_ALPHA +
(scaleFactor - MIN_SCALE) /
( - MIN_SCALE) * ( - MIN_ALPHA)); } else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha();
}
}
}