Android项目实战(三):实现第一次进入软件的引导页
最近做的APP接近尾声了,就是些优化工作了,
我们都知道现在的APP都会有引导页,就是安装之后第一次打开才显示的引导页面(介绍这个软件的几张可以切换的图)
自己做了一下,结合之前学过的
思路很简单,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
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 }
作者:听着music睡
出处:http://www.cnblogs.com/xqxacm/
Android交流群:38197636
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。