Android:仿微信开场切换界面
这实例很多人仿做,好实例还是不容错过!最重要是素材容易拿~
效果:
默认3页面的切换,最后一个页面带按钮,点击进入另外一个页面
思路:
1、准备5个布局页面,1个为主函数布局页面,3个为切换的页面(其一带按钮),1个为按钮点击进入的页面(这个需要Activity)。
2、用适配器绑定相应的view,设置适配器
3、制作点点,设置监听事件控制点点
4、设置按钮的点击事件
主要代码:
主布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="wrap_content" > </android.support.v4.view.ViewPager> </LinearLayout>
有按钮的切换页布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/whatsnew_page01" > <Button android:id="@+id/comein" android:layout_width="120dp" android:layout_height="wrap_content" android:padding="10dp" android:text="进入微信" android:textColor="#ffffff" android:layout_centerInParent="true" android:background="@drawable/btn_selector" android:onClick="startbutton" /> </RelativeLayout>
程序代码:
1 public class MainActivity extends Activity { 2 private ViewPager viewpager; 3 private ArrayList<View>views ; 4 5 //点点 6 private ViewGroup listgroup; 7 private ImageView imageview; 8 private ImageView[] imageviewArr; 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 //设置无标题栏 14 requestWindowFeature(Window.FEATURE_NO_TITLE); 15 16 setContentView(R.layout.start); 17 18 //内容 19 viewpager = (ViewPager) findViewById(R.id.viewpager); 20 views = new ArrayList<View>(); 21 22 23 //把view加入数组 24 LayoutInflater lf = getLayoutInflater(); 25 View v1 =lf.inflate(R.layout.w1, null); 26 View v2 =lf.inflate(R.layout.w2, null); 27 View v3 =lf.inflate(R.layout.w3, null); 28 views.add(v1); 29 views.add(v2); 30 views.add(v3); 31 32 33 /* 34 * 把点加入ViewGroup 35 * */ 36 listgroup = (ViewGroup) findViewById(R.id.listgroup); 37 //有多少个界面数组就包含多少个点 38 imageviewArr = new ImageView[views.size()]; 39 //生成每个点 40 for(int i=0;i<views.size();i++){ 41 imageview = new ImageView(MainActivity.this); 42 //设置点的宽和高 43 imageview.setLayoutParams(new LayoutParams(9,9)); 44 //设置点padding(左,上,右,下) 45 imageview.setPadding(20, 0, 20, 0); 46 47 48 //放入数组 49 imageviewArr[i]=imageview; 50 51 //设置第一个点样式为选中状态,其他为默认状态 52 if(i==0){ 53 imageview.setBackgroundResource(R.drawable.page_indicator_focused); 54 }else{ 55 imageview.setBackgroundResource(R.drawable.page_indicator_unfocused); 56 } 57 58 listgroup.addView(imageviewArr[i]); 59 60 } 61 62 63 //绑定适配器 64 viewpager.setAdapter(new ViewPagerAdapter()); 65 66 //设置监听器,切换时,点样式同步切换 67 viewpager.setOnPageChangeListener(new listChangerListener()); 68 } 69 70 71 72 73 /* 74 * viewpager的适配器 75 * */ 76 public class ViewPagerAdapter extends PagerAdapter{ 77 //实现PagerAdapter至少要覆盖以下方法 78 @Override 79 //返回窗体界面数 80 public int getCount() { 81 // TODO Auto-generated method stub 82 return views.size(); 83 } 84 85 @Override 86 //判断对象是否生成界面 87 public boolean isViewFromObject(View arg0, Object arg1) { 88 // TODO Auto-generated method stub 89 return arg0==arg1; 90 } 91 92 93 @Override 94 //销毁一个界面 95 public void destroyItem(View container, int position, Object object) { 96 // TODO Auto-generated method stub 97 ((ViewPager)container).removeView(views.get(position)); 98 } 99 100 @Override 101 //初始化界面 102 public Object instantiateItem(View container, int position) { 103 // TODO Auto-generated method stub 104 ((ViewPager)container).addView(views.get(position)); 105 return views.get(position); 106 } 107 108 } 109 110 111 /* 112 * viewpager的监听器 113 * */ 114 public class listChangerListener implements OnPageChangeListener{ 115 116 @Override 117 public void onPageScrollStateChanged(int arg0) { 118 // TODO Auto-generated method stub 119 120 } 121 122 @Override 123 public void onPageScrolled(int arg0, float arg1, int arg2) { 124 // TODO Auto-generated method stub 125 126 } 127 128 @Override 129 public void onPageSelected(int arg0) { 130 // TODO Auto-generated method stub 131 for(int i=0;i<imageviewArr.length;i++){ 132 //当前索引为arg0,循环所有点,如果i为当前索引。则i样式为选中状态 133 imageviewArr[arg0].setBackgroundResource(R.drawable.page_indicator_focused); 134 if(arg0 !=i){ 135 //否则样式为未选中状态 136 imageviewArr[i].setBackgroundResource(R.drawable.page_indicator_unfocused); 137 } 138 } 139 140 } 141 142 } 143 144 //点击按钮打开新页面 145 public void startbutton(View v) { 146 Intent intent = new Intent(MainActivity.this, HomeActivity.class); 147 startActivity(intent); 148 MainActivity.this.finish(); 149 } 150 151 152 153 }
2024还活着,挺好的,向着星辰与深渊,加油,博客园不要倒下啊!