首页 > 代码库 > 【转】android 欢迎界面翻页成效,仿微信第一次登陆介绍翻页界面

【转】android 欢迎界面翻页成效,仿微信第一次登陆介绍翻页界面

android 欢迎界面翻页效果,仿微信第一次登陆介绍翻页界面

 

本实例做的相对比较简单主要是对翻页控件的使用,有时候想要做一些功能是主要是先了解下是否有现成的控件可以使用,做起来比较简单不用费太大的劲去找别的资料,或者别的办法设计。有空多读读android API了解熟悉了做什么都比较容易。(注意:ViewPager用于实现多页面的切换效果,该类存在于Google的兼容包里面,所以在引用时记得在BuilldPath中加入“android-support-v4.jar” 如果sdk是4.0及以上的都包含了该包)本例步骤如下:

 

第一步:main.xml设计,其中ViewPager为多页显示控件,PagerTitleStrip用于显示当前页面的标题,下面的imagee是圆点展示当前页状态和总页数:

 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >      <android.support.v4.view.ViewPager        android:id="@+id/whatsnew_viewpager"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center" >                 <!--如果不需要显示什么标题的话可以去点这个控件这里是为了连接控件的作用所以添加上去的  -->        <android.support.v4.view.PagerTitleStrip            android:id="@+id/pagertitle"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="bottom"            android:background="@null"            android:textColor="#3399cc" />    </android.support.v4.view.ViewPager>                       <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_gravity="bottom"        	android:layout_marginBottom="30dp"         	android:gravity="center_horizontal"     >                        <ImageView            	android:id="@+id/page0"        	 android:layout_width="wrap_content"        	 android:layout_height="wrap_content"        	             	 android:scaleType="matrix"        	 android:src="http://www.mamicode.com/@drawable/page_now" />            <ImageView            	android:id="@+id/page1"        	 android:layout_width="wrap_content"        	 android:layout_height="wrap_content"        	 android:layout_marginLeft="10dp"        	             	 android:scaleType="matrix"        	 android:src="http://www.mamicode.com/@drawable/page" />            <ImageView            	android:id="@+id/page2"        	 android:layout_width="wrap_content"        	 android:layout_height="wrap_content"        	 android:layout_marginLeft="10dp"           	             	 android:scaleType="matrix"        	 android:src="http://www.mamicode.com/@drawable/page" />            <ImageView            	android:id="@+id/page3"        	 android:layout_width="wrap_content"        	 android:layout_height="wrap_content"         	 android:layout_marginLeft="10dp"          	             	 android:scaleType="matrix"        	 android:src="http://www.mamicode.com/@drawable/page" />            <ImageView            	android:id="@+id/page4"        	 android:layout_width="wrap_content"        	 android:layout_height="wrap_content"         	 android:layout_marginLeft="10dp"          	             	 android:scaleType="matrix"        	 android:src="http://www.mamicode.com/@drawable/page" />            <ImageView            	android:id="@+id/page5"        	 android:layout_width="wrap_content"        	 android:layout_height="wrap_content"         	 android:layout_marginLeft="10dp"          	             	 android:scaleType="matrix"        	 android:src="http://www.mamicode.com/@drawable/page" />                       <ImageView            	android:id="@+id/page6"        	 android:layout_width="wrap_content"        	 android:layout_height="wrap_content"         	 android:layout_marginLeft="10dp"          	             	 android:scaleType="matrix"        	 android:src="http://www.mamicode.com/@drawable/page" />         </LinearLayout>           </FrameLayout>
 

 

 

 

第二步:创建MyPagerAdapter 继承PagerAdapter这个适配器比较简单,如下:

 

import java.util.ArrayList;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.view.View;public class MyPagerAdapter extends PagerAdapter{private ArrayList<View> views;private ArrayList<String> titles;public MyPagerAdapter(ArrayList<View> views,ArrayList<String> titles){this.views = views;this.titles = titles;}@Overridepublic int getCount() {return this.views.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1;}public void destroyItem(View container, int position, Object object) {((ViewPager)container).removeView(views.get(position));}//标题, 如果不要标题可以去掉这里@Override    public CharSequence getPageTitle(int position) {        return titles.get(position);    }//页面viewpublic Object instantiateItem(View container, int position) {((ViewPager)container).addView(views.get(position));return views.get(position);}}
 

 

 

第三步:编写MainActivity :

 

import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.support.v4.view.PagerTitleStrip;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.view.LayoutInflater;import android.view.View;import android.view.Window;import android.widget.ImageView;public class MainActivity extends Activity {//翻页控件private ViewPager mViewPager;private PagerTitleStrip mPagerTitleStrip;//这5个是底部显示当前状态点imageViewprivate ImageView mPage0;private ImageView mPage1;private ImageView mPage2;private ImageView mPage3;private ImageView mPage4;private ImageView mPage5;private ImageView mPage6;    @Override    public void onCreate(Bundle savedInstanceState) {    	//去掉标题栏全屏显示    	requestWindowFeature(Window.FEATURE_NO_TITLE);        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                mViewPager = (ViewPager)findViewById(R.id.whatsnew_viewpager);        mPagerTitleStrip = (PagerTitleStrip)findViewById(R.id.pagertitle);                        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());                mPage0 = (ImageView)findViewById(R.id.page0);        mPage1 = (ImageView)findViewById(R.id.page1);        mPage2 = (ImageView)findViewById(R.id.page2);        mPage3 = (ImageView)findViewById(R.id.page3);        mPage4 = (ImageView)findViewById(R.id.page4);        mPage5 = (ImageView)findViewById(R.id.page5);        mPage6 = (ImageView)findViewById(R.id.page6);                     /*       * 这里是每一页要显示的布局,根据应用需要和特点自由设计显示的内容       * 以及需要显示多少页等       */        LayoutInflater mLi = LayoutInflater.from(this);        View view1 = mLi.inflate(R.layout.whats_news_gallery_one, null);        View view2 = mLi.inflate(R.layout.whats_news_gallery_two, null);        View view3 = mLi.inflate(R.layout.whats_news_gallery_three, null);        View view4 = mLi.inflate(R.layout.whats_news_gallery_four, null);        View view5 = mLi.inflate(R.layout.whats_news_gallery_five, null);        View view6 = mLi.inflate(R.layout.whats_news_gallery_six, null);        View view7 = mLi.inflate(R.layout.whats_news_gallery_seven, null);      	/*      	 * 这里将每一页显示的view存放到ArrayList集合中      	 * 可以在ViewPager适配器中顺序调用展示      	 */        final ArrayList<View> views = new ArrayList<View>();        views.add(view1);        views.add(view2);        views.add(view3);        views.add(view4);        views.add(view5);        views.add(view6);           views.add(view7);                         /*      	 * 每个页面的Title数据存放到ArrayList集合中      	 * 可以在ViewPager适配器中调用展示      	 */        final ArrayList<String> titles = new ArrayList<String>();        titles.add("tab1");        titles.add("tab2");        titles.add("tab3");        titles.add("tab4");        titles.add("tab5");        titles.add("tab6");        titles.add("tab7");                //填充ViewPager的数据适配器        MyPagerAdapter mPagerAdapter = new MyPagerAdapter(views,titles);mViewPager.setAdapter(mPagerAdapter);    }            public class MyOnPageChangeListener implements OnPageChangeListener {        public void onPageSelected(int page) {//翻页时当前page,改变当前状态园点图片switch (page) {case 0:mPage0.setImageDrawable(getResources().getDrawable(R.drawable.page_now));mPage1.setImageDrawable(getResources().getDrawable(R.drawable.page));break;case 1:mPage1.setImageDrawable(getResources().getDrawable(R.drawable.page_now));mPage0.setImageDrawable(getResources().getDrawable(R.drawable.page));mPage2.setImageDrawable(getResources().getDrawable(R.drawable.page));break;case 2:mPage2.setImageDrawable(getResources().getDrawable(R.drawable.page_now));mPage1.setImageDrawable(getResources().getDrawable(R.drawable.page));mPage3.setImageDrawable(getResources().getDrawable(R.drawable.page));break;case 3:mPage3.setImageDrawable(getResources().getDrawable(R.drawable.page_now));mPage4.setImageDrawable(getResources().getDrawable(R.drawable.page));mPage2.setImageDrawable(getResources().getDrawable(R.drawable.page));break;case 4:mPage4.setImageDrawable(getResources().getDrawable(R.drawable.page_now));mPage3.setImageDrawable(getResources().getDrawable(R.drawable.page));mPage5.setImageDrawable(getResources().getDrawable(R.drawable.page));break;case 5:mPage5.setImageDrawable(getResources().getDrawable(R.drawable.page_now));mPage4.setImageDrawable(getResources().getDrawable(R.drawable.page));mPage6.setImageDrawable(getResources().getDrawable(R.drawable.page));break;case 6:mPage6.setImageDrawable(getResources().getDrawable(R.drawable.page_now));mPage5.setImageDrawable(getResources().getDrawable(R.drawable.page));break;}}public void onPageScrolled(int arg0, float arg1, int arg2) {}public void onPageScrollStateChanged(int arg0) {}}        }
 

 

第四步;运行效果如图底部是标题栏:




 

 

去掉底部是标题栏如下效果: