400-680-6399

Android应用中的开机引导界面——用ViewPager实现

2016-03-17 12:16 来源:未知
摘要:这篇文章中,只介绍第一种实现方式 ViewPager , 第二种方式将会在下一篇博文中介绍; ViewPager 组件是在Android 11以后才出来的一个组件,位于:android-support-v4.jar 中,因此使用时需要进入
这篇文章中,只介绍第一种实现方式 ViewPager , 第二种方式将会在下一篇博文中介绍;
ViewPager 组件是在Android 11以后才出来的一个组件,位于:android-support-v4.jar 中,因此使用时需要进入此包。
第一步:在配置文件中,引入ViewPager ,代码如下:

    <font face="宋体" size="3"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

        xmlns:tools="http://schemas.android.com/tools"  

        android:layout_width="match_parent"  

        android:layout_height="match_parent"  

        tools:context=".MainActivity" >  

      

        <android.support.v4.view.ViewPager  

            android:id="@+id/pager"  

            android:layout_width="match_parent"  

            android:layout_height="match_parent"  

            />  

      

    </RelativeLayout>  </font>

复制代码

    <font face="宋体" size="3">package com.zhanggeng.guider;  

      

    import java.util.ArrayList;  

      

    import android.app.Activity;  

    import android.content.Context;  

    import android.content.SharedPreferences;  

    import android.os.Bundle;  

    import android.support.v4.view.PagerAdapter;  

    import android.support.v4.view.ViewPager;  

    import android.view.Menu;  

    import android.view.View;  

    import android.view.ViewGroup;  

    import android.view.ViewGroup.LayoutParams;  

    import android.view.Window;  

    import android.widget.ImageView;  

    import android.widget.ImageView.ScaleType;  

      

    import com.zhanggeng.myutil.dialogtool.DialogTool;  

      

    public class MainActivity extends Activity {  

      

        //Viewpager对象  

        private ViewPager viewPager;  

        //ViewPager适配器对象  

        private GuiderPagerAdapter adapter;  

        //SharedPreferences 用来判断是否是第一次登陆,第一次登陆则显示引导界面,否则直接进入。  

        private SharedPreferences pre;  

        private SharedPreferences.Editor editor;  

        //判断标记  

        private boolean isFirst;  

        Context context = this;  

        @Override  

        protected void onCreate(Bundle savedInstanceState) {  

            super.onCreate(savedInstanceState);  

            //设置无标题  

            requestWindowFeature(Window.FEATURE_NO_TITLE);  

            //设置SharedPreferences   

            pre = getSharedPreferences("isFirst", MODE_WORLD_WRITEABLE);  

            isFirst = pre.getBoolean("isFirst", true);  

            if(isFirst){  

                //如果是第一次登陆,则初始化,引导界面  

                initView();  

            }else{  

                //否则直接进入  

                gotoLoginActivity();  

            }  

        }  

        private void initView() {  

            //加载程序布局界面  

            setContentView(R.layout.activity_main);  

            //为程序设置是否第一次登入的标记  

            saveTag();  

              

            viewPager = (ViewPager) findViewById(R.id.pager);  

            adapter = new GuiderPagerAdapter();  

            //初始化每一个页面  

            ImageView v1 = new ImageView(this);  

            v1.setLayoutParams(new LayoutParams(1, -1));  

            v1.setScaleType(ScaleType.FIT_XY);  

            v1.setImageResource(R.drawable.guild1);  

            adapter.views.add(v1);  

              

            ImageView v2 = new ImageView(this);  

            v2.setLayoutParams(new LayoutParams(-1 , -1));  

            v2.setScaleType(ScaleType.FIT_XY);  

            v2.setImageResource(R.drawable.guild2);  

            adapter.views.add(v2);  

              

            ImageView v3 = new ImageView(this);  

            v3.setLayoutParams(new LayoutParams(-1, -1));  

            v3.setScaleType(ScaleType.FIT_XY);  

            v3.setImageResource(R.drawable.guild3);  

            adapter.views.add(v3);  

            //为最后一个页面添加监听事件,当点击后,进入主页面  

            v3.setOnClickListener(new View.OnClickListener() {  

                @Override  

                public void onClick(View v) {  

                    // TODO Auto-generated method stub  

                    gotoLoginActivity();  

                }  

            });  

            viewPager.setAdapter(adapter);  

        }  

        private void saveTag() {  

            // TODO Auto-generated method stub  

            editor = pre.edit();  

            editor.putBoolean("isFirst", false);  

            editor.commit();  

        }  

        private void gotoLoginActivity() {  

            // TODO Auto-generated method stub  

            DialogTool.createOnlyButtonDialog(context, "OK", "You have login success !", "LoginSuccess", null);  

        }  

        class GuiderPagerAdapter extends PagerAdapter{  

            public ArrayList<View> views;  

            public GuiderPagerAdapter(){  

                views = new ArrayList<View>();  

            }  

            @Override  

            public int getCount() {  

                // TODO Auto-generated method stub  

                return views.size();  

            }  

            @Override  

            public void destroyItem(ViewGroup container, int position, Object object) {  

                // TODO Auto-generated method stub  

                container.removeView(views.get(position));  

            }  

            @Override  

            public Object instantiateItem(ViewGroup container, int position) {  

                // TODO Auto-generated method stub  

                container.addView(views.get(position));  

                return views.get(position);  

            }  

            @Override  

            public boolean isViewFromObject(View arg0, Object arg1) {  

                // TODO Auto-generated method stub  

                return arg0 == arg1;  

            }  

        }  

    }  </font>
热门标签:

热门课程

相关文章