ViewPager(4)用viewpager实现splash view
1,示例
2,代码
SplashMain.java
1 import android.os.Bundle; 2 import android.support.v4.app.Fragment; 3 import android.support.v4.view.ViewPager; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.LinearLayout; 8 9 import com.txw.e.viewpager.R; 10 import com.txw.e.viewpager.main.MainActivity; 11 12 /** 13 * A simple {@link Fragment} subclass. 14 */ 15 public class SplashMain extends Fragment { 16 17 18 //1,在layout.xml中添加 ViewPagerFragment,它可以是顶级布局,如下: 19 /* 20 <?xml version="1.0" encoding="utf-8"?> 21 <android.support.v4.view.ViewPager 22 android:id="@+id/splash_pager" 23 xmlns:android="http://schemas.android.com/apk/res/android" 24 android:layout_width="match_parent" 25 android:layout_height="match_parent"/> 26 */ 27 28 //2,准备变量,ViewPager和 PagerAdapter ,OnPageChangeListener 29 ViewPager pager; 30 SplashViewPagerAdapter pagerAdapter; 31 32 //3,初始化pager 33 void initPager(View v){ 34 //从layout.xml中初始化pager 35 pager = (ViewPager) v.findViewById(R.id.splash_pager); 36 37 //初始化page adapter 38 pagerAdapter = new SplashViewPagerAdapter(getFragmentManager()); 39 40 //设置adapter 41 pager.setAdapter(pagerAdapter); 42 43 //设置page切换监听者 44 pager.addOnPageChangeListener(onPageChangeListener); 45 46 //设置pager切换动画 47 // pager.setPageTransformer(true, new DepthPageTransformer()); 48 49 50 } 51 52 //4,处理 pager 切换事件 53 //这里用的ViewPager.SimpleOnPageChangeListener,它什么也不做。 54 ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() { 55 @Override 56 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 57 if (positionOffset == 0){ 58 for (int i = 0; i < indexer.getChildCount(); ++i){ 59 View v = indexer.getChildAt(i); 60 if (i == position){ 61 v.setBackgroundResource(R.drawable.splash_indexer_focused); 62 }else{ 63 v.setBackgroundResource(R.drawable.splash_indexer); 64 } 65 } 66 } 67 } 68 69 @Override 70 public void onPageSelected(int position) { 71 72 } 73 74 @Override 75 public void onPageScrollStateChanged(int state) { 76 77 } 78 }; 79 80 //5,重写切换动画类,可以把这个类放到外面去。 81 public class DepthPageTransformer implements ViewPager.PageTransformer { 82 private static final float MIN_SCALE = 0.75f; 83 84 public void transformPage(View view, float position) { 85 int pageWidth = view.getWidth(); 86 87 if (position < -1) { // [-Infinity,-1) 88 // This page is way off-screen to the left. 89 view.setAlpha(0); 90 91 } else if (position <= 0) { // [-1,0] 92 // Use the default slide transition when moving to the left page 93 view.setAlpha(1); 94 view.setTranslationX(0); 95 view.setScaleX(1); 96 view.setScaleY(1); 97 98 } else if (position <= 1) { // (0,1] 99 // Fade the page out. 100 view.setAlpha(1 - position); 101 102 // Counteract the default slide transition 103 view.setTranslationX(pageWidth * -position); 104 105 // Scale the page down (between MIN_SCALE and 1) 106 float scaleFactor = MIN_SCALE 107 + (1 - MIN_SCALE) * (1 - Math.abs(position)); 108 view.setScaleX(scaleFactor); 109 view.setScaleY(scaleFactor); 110 111 } else { // (1,+Infinity] 112 // This page is way off-screen to the right. 113 view.setAlpha(0); 114 } 115 } 116 } 117 118 119 LinearLayout indexer; 120 121 122 @Override 123 public View onCreateView(LayoutInflater inflater, ViewGroup container, 124 Bundle savedInstanceState) { 125 // Inflate the layout for this fragment 126 View v = inflater.inflate(R.layout.fragment_splash_main, container, false); 127 128 initPager(v); 129 130 indexer = (LinearLayout) v.findViewById(R.id.splash_indexer); 131 132 return v; 133 } 134 135 @Override 136 public void onCreate(Bundle savedInstanceState) { 137 super.onCreate(savedInstanceState); 138 MainActivity aty = (MainActivity) getActivity(); 139 aty.getSupportActionBar().hide(); 140 } 141 142 @Override 143 public void onDestroy() { 144 super.onDestroy(); 145 MainActivity aty = (MainActivity) getActivity(); 146 aty.getSupportActionBar().show(); 147 } 148 }
SplashViewPagerAdapter.java
1 import android.support.v4.app.Fragment; 2 import android.support.v4.app.FragmentManager; 3 import android.support.v4.app.FragmentStatePagerAdapter; 4 5 public class SplashViewPagerAdapter extends FragmentStatePagerAdapter { 6 7 8 public SplashViewPagerAdapter(FragmentManager fm) { 9 super(fm); 10 11 } 12 13 @Override 14 public Fragment getItem(int position) { 15 SplashFragment fragment = new SplashFragment(); 16 fragment.colorId = position; 17 return fragment; 18 } 19 20 @Override 21 public int getCount() { 22 return 4; 23 } 24 }
SplashFragment.java
1 public class SplashFragment extends Fragment { 2 3 4 public int colors[] = {Color.BLUE,Color.CYAN,Color.GREEN,Color.MAGENTA}; 5 6 public View rootView; 7 public int colorId; 8 9 View.OnClickListener clickListener = new View.OnClickListener(){ 10 11 @Override 12 public void onClick(View v) { 13 if (colorId == 3){ 14 getFragmentManager().popBackStack(); 15 } 16 } 17 }; 18 19 @Override 20 public View onCreateView(LayoutInflater inflater, ViewGroup container, 21 Bundle savedInstanceState) { 22 // Inflate the layout for this fragment 23 rootView = inflater.inflate(R.layout.fragment_splash, container, false); 24 25 rootView.setBackgroundColor(colors[colorId]); 26 27 rootView.setOnClickListener(clickListener); 28 29 return rootView; 30 } 31 32 }
3,xml
fragment_splash_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="#FFFFFF"> 7 8 <android.support.v4.view.ViewPager 9 android:id="@+id/splash_pager" 10 android:layout_width="match_parent" 11 android:layout_height="match_parent"/> 12 13 <LinearLayout 14 android:id="@+id/splash_indexer" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:layout_gravity="bottom|center_horizontal" 18 android:layout_marginBottom="30dp" 19 android:orientation="horizontal"> 20 21 <TextView 22 android:layout_width="wrap_content" 23 android:layout_height="match_parent" 24 android:layout_gravity="center_horizontal" 25 android:background="@drawable/splash_indexer"/> 26 27 <TextView 28 android:layout_width="wrap_content" 29 android:layout_height="match_parent" 30 android:layout_gravity="center_horizontal" 31 android:background="@drawable/splash_indexer"/> 32 33 <TextView 34 android:layout_width="wrap_content" 35 android:layout_height="match_parent" 36 android:layout_gravity="center_horizontal" 37 android:background="@drawable/splash_indexer"/> 38 39 <TextView 40 android:layout_width="wrap_content" 41 android:layout_height="match_parent" 42 android:layout_gravity="center_horizontal" 43 android:background="@drawable/splash_indexer"/> 44 </LinearLayout> 45 46 </FrameLayout>
fragment_splash.xml
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#FFFFFF" 6 tools:context="com.txw.e.viewpager.splash.SplashFragment"> 7 </FrameLayout>
drawable/splash_indexer_focused.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" 3 android:shape="oval"> 4 <!--<gradient--> 5 <!--android:endColor="#f793ac"--> 6 <!--android:gradientRadius="50"--> 7 <!--android:startColor="#fa1621"--> 8 <!--android:type="radial"/>--> 9 10 <solid android:color="#FFFFFF" /> 11 <size android:width="20dp" android:height="20dp"/> 12 13 </shape>
drawable/splash_indexer.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" 3 android:shape="oval"> 4 <gradient 5 android:endColor="#fa1621" 6 android:gradientRadius="50" 7 android:startColor="#f793ac" 8 android:type="radial"/> 9 <size 10 android:width="20dp" 11 android:height="20dp"/> 12 13 </shape>