天天看點

Android項目實戰(三):實作第一次進入軟體的引導頁

最近做的APP接近尾聲了,就是些優化工作了,

我們都知道現在的APP都會有引導頁,就是安裝之後第一次打開才顯示的引導頁面(介紹這個軟體的幾張可以切換的圖)

自己做了一下,結合之前學過的

慕課網_ViewPager切換動畫(3.0版本以上有效果)

思路很簡單,APP的主界面還是作為主Activity,隻要新添加一個類來判斷是不是第一次打開APP

設主activity 名字為:MainActivity.java   判斷是不是第一次打開APP且實作引導頁面的類 LoginActivity ,另外還需要一個類 這是

安卓開發_慕課網_ViewPager切換動畫(3.0版本以上有效果)

中用到的 ZoomOutPageTransformer.java

1.在主activity中,在一開始 先跳轉到LoginActivity類中

1  protected void onCreate(Bundle savedInstanceState) {
 2         super.onCreate(savedInstanceState);
 3         requestWindowFeature(Window.FEATURE_NO_TITLE);
 4         setContentView(R.layout.activity_main);
 5         startActivity(new Intent(MainActivity.this, LoginActivity.class)); //跳轉到LoginActivity類中
 6       //初始化
 7         init();
 8         initEvent();
 9         //預設顯示第一個功能的界面(微信界面)
10         setSelect(0);
11     }      

2.在LoginActivity類中判斷是不是第一次打開,如果是,則顯示ViewPager切換動畫效果

如果不是,則執行finish();語句,直接結束這個Activity  自然的就回到了主Activity中了

LoginActivity代碼

1 package com.example.qunxiong;
 2 
 3 import java.io.OutputStream;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 
 8 
 9 import android.app.Activity;
10 import android.content.Context;
11 import android.content.Intent;
12 import android.content.SharedPreferences;
13 import android.content.SharedPreferences.Editor;
14 import android.os.Bundle;
15 import android.support.v4.view.PagerAdapter;
16 import android.support.v4.view.ViewPager;
17 import android.view.View;
18 import android.view.ViewGroup;
19 import android.view.View.OnClickListener;
20 import android.widget.Button;
21 import android.widget.ImageView;
22 import android.widget.ImageView.ScaleType;
23 
24 public class LoginActivity extends Activity {
25     private ViewPager mviewpager; //控件
26     private int[] mImgIds = new int[]{R.drawable.bg_rank_shenqi_0,R.drawable.bg_rank_shenqi_1,R.drawable.bg_rank_shenqi_10};//初始化圖檔
27     private List<ImageView> mImages = new ArrayList<ImageView>();
28     private Button jump;
29      private SharedPreferences preferences;
30       private Editor editor;
31       private OutputStream os;
32       @Override
33       protected void onCreate(Bundle savedInstanceState) {
34        super.onCreate(savedInstanceState);
35        setContentView(R.layout.login);
36        preferences = getSharedPreferences("phone", Context.MODE_PRIVATE);
37        jump = (Button) findViewById(R.id.jump);
38        jump.setOnClickListener(new OnClickListener() {    
39             @Override
40             public void onClick(View v) {
41                 // TODO Auto-generated method stub
42                 finish();
43             }
44         });
45        //判斷是不是首次登入,
46        if (preferences.getBoolean("firststart", true)) {
47         editor = preferences.edit();
48         //将登入标志位設定為false,下次登入時不在顯示首次登入界面
49         editor.putBoolean("firststart", false);
50         editor.commit();
51         mviewpager = (ViewPager) findViewById(R.id.id_viewpager);
52         //為ViewPage切換添加動畫效果(3.0以上版本才可以有效果)
53         mviewpager.setPageTransformer(true, new ZoomOutPageTransformer());
54         
55         mviewpager.setAdapter(new PagerAdapter() {
56             
57             @Override
58             public Object instantiateItem(View container, int position) {
59                 ImageView imageview = new ImageView(LoginActivity.this);
60                 imageview.setImageResource(mImgIds[position]);
61                 imageview.setScaleType(ScaleType.CENTER_CROP); //設定圖檔不變形
62                 ((ViewGroup) container).addView(imageview);
63                 mImages.add(imageview);
64                 return imageview;
65             };
66             @Override
67             public void destroyItem(View container, int position, Object object) {
68                 // TODO Auto-generated method stub
69                 
70                 ((ViewPager) container).removeView(mImages.get(position));
71             }
72             public boolean isViewFromObject(View view, Object object) {
73                 // TODO Auto-generated method stub
74                 return view == object;
75             }
76             
77             @Override
78             public int getCount() {                   //傳回圖檔個數
79                 // TODO Auto-generated method stub
80                 return mImgIds.length;
81             }
82         });
83        }
84        else 
85        {
86            finish();
87        }
88        
89       }
90 }
91             

 切換動畫效果代碼 

ZoomOutPageTransformer.java
      
Android項目實戰(三):實作第一次進入軟體的引導頁
Android項目實戰(三):實作第一次進入軟體的引導頁
1 package com.example.qunxiong;
 2 
 3 import android.annotation.SuppressLint;
 4 import android.support.v4.view.ViewPager;
 5 import android.view.View;
 6 
 7 public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
 8     private static final float MIN_SCALE = 0.85f;
 9     private static final float MIN_ALPHA = 0.5f;
10     
11     @SuppressLint("NewApi") public void transformPage(View view, float position) {
12         int pageWidth = view.getWidth();
13         int pageHeight = view.getHeight();
14 
15         if (position < -1) { // [-Infinity,-1)
16             // This page is way off-screen to the left.
17             view.setAlpha(0);
18 
19         } else if (position <= 1) { // [-1,1]
20             // Modify the default slide transition to shrink the page as well
21             float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
22             float vertMargin = pageHeight * (1 - scaleFactor) / 2;
23             float horzMargin = pageWidth * (1 - scaleFactor) / 2;
24             if (position < 0) {
25                 view.setTranslationX(horzMargin - vertMargin / 2);
26             } else {
27                 view.setTranslationX(-horzMargin + vertMargin / 2);
28             }
29 
30             // Scale the page down (between MIN_SCALE and 1)
31             view.setScaleX(scaleFactor);
32             view.setScaleY(scaleFactor);
33 
34             // Fade the page relative to its size.
35             view.setAlpha(MIN_ALPHA +
36                     (scaleFactor - MIN_SCALE) /
37                     (1 - MIN_SCALE) * (1 - MIN_ALPHA));
38 
39         } else { // (1,+Infinity]
40             // This page is way off-screen to the right.
41             view.setAlpha(0);
42         }
43     }
44 }      

View Code

繼續閱讀