android 第一次运行应用的引导界面

先看看效果图:

android 第一次运行应用的引导界面

android 第一次运行应用的引导界面

主要代码:

package com.example.introducedemo;



import java.util.ArrayList;



import android.app.Activity;

import android.os.Bundle;

import android.os.Parcelable;

import android.support.v4.view.PagerAdapter;

import android.support.v4.view.ViewPager;

import android.support.v4.view.ViewPager.OnPageChangeListener;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup.LayoutParams;

import android.view.Window;

import android.widget.ImageView;

import android.widget.LinearLayout;



public class MainActivity extends Activity {

    private ImageView mImageView;

    View mView;

    private ArrayList<View> mPageViews;

    // 定义LayoutInflater

    LayoutInflater mInflater;

    private ViewPager mViewPager;

    LinearLayout mLinearLayout;

    private ImageView[] mImageViews;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // 设置窗口无标题

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        mInflater = getLayoutInflater();



        mPageViews = new ArrayList<View>();

        mPageViews.add(mInflater.inflate(R.layout.view_01, null));

        mPageViews.add(mInflater.inflate(R.layout.view_02, null));

        mPageViews.add(mInflater.inflate(R.layout.view_03, null));

        mImageViews = new ImageView[mPageViews.size()];

        mView = mInflater.inflate(R.layout.activity_main, null);

        mViewPager = (ViewPager) mView.findViewById(R.id.myviewpager);

        mLinearLayout = (LinearLayout) mView

                .findViewById(R.id.mybottomviewgroup);

        for (int i = 0; i < mImageViews.length; i++) {

            mImageView = new ImageView(MainActivity.this);

            mImageView.setLayoutParams(new LayoutParams(10, 10));

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(

                    LinearLayout.LayoutParams.WRAP_CONTENT,

                    LinearLayout.LayoutParams.WRAP_CONTENT);

            lp.setMargins(10, 0, 10, 0);

            mImageView.setLayoutParams(lp);



            if (i == 0) {

                mImageView.setBackgroundResource(R.drawable.page_indicator_focused);

            } else {

                mImageView

                        .setBackgroundResource(R.drawable.page_indicator_unfocused);

            }



            mImageViews[i] = mImageView;



            // 把指示作用的远点图片加入底部的视图中

            mLinearLayout.addView(mImageViews[i]);

        }

        setContentView(mView);

        mViewPager.setAdapter(new MyPagerAdapter());

        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

            public void onPageSelected(int arg0) {

                for (int i = 0; i < mImageViews.length; i++) {

                    if (i == arg0) {

                        mImageViews[i]

                                .setBackgroundResource(R.drawable.page_indicator_focused);

                    } else {

                        mImageViews[i]

                                .setBackgroundResource(R.drawable.page_indicator_unfocused);

                    }

                }

            }



            @Override

            public void onPageScrolled(int arg0, float arg1, int arg2) {

                // TODO Auto-generated method stub



            }



            @Override

            public void onPageScrollStateChanged(int arg0) {

                // TODO Auto-generated method stub



            }

        });

    }



    class MyPagerAdapter extends PagerAdapter {



        @Override

        public int getCount() {

            return mPageViews.size();

        }



        @Override

        public boolean isViewFromObject(View arg0, Object arg1) {

            return arg0 == arg1;

        }



        @Override

        public int getItemPosition(Object object) {

            // TODO Auto-generated method stub

            return super.getItemPosition(object);

        }



        @Override

        public void destroyItem(View arg0, int arg1, Object arg2) {

            // TODO Auto-generated method stub

            ((ViewPager) arg0).removeView(mPageViews.get(arg1));

        }



        @Override

        public Object instantiateItem(View arg0, int arg1) {

            // TODO Auto-generated method stub

            ((ViewPager) arg0).addView(mPageViews.get(arg1));

            return mPageViews.get(arg1);

        }



        @Override

        public void restoreState(Parcelable arg0, ClassLoader arg1) {

            // TODO Auto-generated method stub



        }



        @Override

        public Parcelable saveState() {

            // TODO Auto-generated method stub

            return null;

        }



        @Override

        public void startUpdate(View arg0) {

            // TODO Auto-generated method stub



        }



        @Override

        public void finishUpdate(View arg0) {

            // TODO Auto-generated method stub



        }



    }

}

CSDN下载地址:http://download.csdn.net/detail/wenwei19861106/4860072

你可能感兴趣的:(android)