功能实现源码 package com.xhm.meishi; import java.util.ArrayList; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Handler; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; public class Loading_Pager implements OnPageChangeListener, OnClickListener { /** * 以下变量需要自定义初始化 */ // 上下文 private Context mContext; // viewpager中圆点资源 private int mPageIndicatorNormal; private int mPageIndicatorFocused; // viewpager图片资源数组资源以及数量 private int mViewPagers[]; // 加载页图片资源 private int mLoadingPager; /** * 以下变量是类的成员变量 */ // handler private Handler mHandler; // viewpager数量 private int mViewPagerSize; // 存储信息 private SharedPreferences mSharedPreferences; // 放圆点和功能展示页的控件 private RelativeLayout mRelativeLayout; // 用户保存圆点的控件 private LinearLayout mLinearLayout; // 添加功能展示页的控件 private ViewPager mViewPager; // 定义圆点的位置 private RelativeLayout.LayoutParams mPointPlace; // 标识是否是第一个登录 private boolean mIsFirstLogin; // 加载页显示时长 private long mLoadingPagerTime; // 圆点控件的大小 private int mPointWidth, mPointHeight; // 圆点的左右间距 private int mPointLeftMargin, mPointRightMargin; // 是否第一次登录的标识 private static final int FIRSTLOGIN = 1; // SharedPreferences中登录标识 private static final String ISFIRSTLOGIN = "firstlogin"; // 应用SharedPreferences名字标识 private static final String PREFERENCESNAME = "com_xhm_user_info"; // handler发送点击标识 public static final int HANDLERID_CLICK = 0x1111; // handler发送倒计时标识 public static final int HANDLERID_TIME = 0x1110; public long getmLoadingPagerTime() { return mLoadingPagerTime; } public void setmLoadingPagerTime(long mLoadingPagerTime) { this.mLoadingPagerTime = mLoadingPagerTime; } public RelativeLayout.LayoutParams getmPointPlace() { return mPointPlace; } public void setmPointPlace(RelativeLayout.LayoutParams mPointPlace) { this.mPointPlace = mPointPlace; } public int getmPointWidth() { return mPointWidth; } public void setmPointWidth(int mPointWidth) { this.mPointWidth = mPointWidth; } public int getmPointHeight() { return mPointHeight; } public void setmPointHeight(int mPointHeight) { this.mPointHeight = mPointHeight; } public int getmPointLeftMargin() { return mPointLeftMargin; } public void setmPointLeftMargin(int mPointLeftMargin) { this.mPointLeftMargin = mPointLeftMargin; } public int getmPointRightMargin() { return mPointRightMargin; } public void setmPointRightMargin(int mPointRightMargin) { this.mPointRightMargin = mPointRightMargin; } /** * * @param context * 上下文 * @param ViewPagerRes * 功能展示页资源数组:R.drawable.xx * @param LoadingPager * 应用加载页:R.drawable.xx * @param PageIndicatorNormal * 标识下边的圆圈:没选择状态 * @param PageIndicatorFocused * 标识下边的圆圈:选择状态 */ public Loading_Pager(Context context, int ViewPagerRes[], int LoadingPager, int PageIndicatorNormal, int PageIndicatorFocused, Handler handler, long time) { // TODO Auto-generated constructor stub initVar(context, ViewPagerRes, LoadingPager, PageIndicatorNormal, PageIndicatorFocused, handler, time); } /** * 初始化成员变量 * * @param sharedPreferencesName */ private void initVar(Context context, int ViewPagerRes[], int LoadingPager, int PageIndicatorNormal, int PageIndicatorFocused, Handler handler, long time) { this.mContext = context; this.mViewPagers = ViewPagerRes; this.mViewPagerSize = mViewPagers.length; this.mLoadingPager = LoadingPager; this.mPageIndicatorNormal = PageIndicatorNormal; this.mPageIndicatorFocused = PageIndicatorFocused; this.mPointHeight = 15; this.mPointWidth = 15; this.mPointLeftMargin = 5; this.mPointRightMargin = 5; this.mLoadingPagerTime = time; this.mHandler = handler; // 定义mLinearLayout的位置 mPointPlace = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); mPointPlace.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); mPointPlace.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); mPointPlace.bottomMargin = 40; mSharedPreferences = mContext.getSharedPreferences(PREFERENCESNAME, 1); mIsFirstLogin = FIRSTLOGIN == mSharedPreferences .getInt(ISFIRSTLOGIN, 1) ? true : false; if (mIsFirstLogin) { Editor mEditor = mSharedPreferences.edit(); mEditor.putInt(ISFIRSTLOGIN, 2); mEditor.commit(); } } /** * 初始化View */ public View getView() { mLinearLayout = new LinearLayout(mContext); initLinearLayout(mLinearLayout); mViewPager = new ViewPager(mContext); initViewPager(mViewPager); mRelativeLayout = new RelativeLayout(mContext); initRelativeLayout(mRelativeLayout, mViewPager, mLinearLayout, mPointPlace); if (!mIsFirstLogin) { mHandler.postDelayed(new Runnable() { public void run() { mHandler.sendEmptyMessage(HANDLERID_TIME); } }, mLoadingPagerTime); } return mRelativeLayout; } /** * 初始化RelativeLayout背景布局 * * @param relativeLayout */ private void initRelativeLayout(RelativeLayout relativeLayout, ViewPager viewpager, LinearLayout linearlayout, LayoutParams pointPlace) { relativeLayout.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); relativeLayout.addView(viewpager); relativeLayout.addView(linearlayout, pointPlace); } /** * 初始化viewpager * * @param viewpager */ private void initViewPager(ViewPager viewpager) { LayoutParams params_viewpager = new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); viewpager.setLayoutParams(params_viewpager); ImageView imageView_viewpager = null; ArrayList<View> pagers = new ArrayList<View>(); int viewpagersize = mIsFirstLogin ? mViewPagerSize : 1; for (int i = 0; i < viewpagersize; i++) { // 展示页添加 imageView_viewpager = new ImageView(mContext); imageView_viewpager.setLayoutParams(params_viewpager); imageView_viewpager .setBackgroundResource(mIsFirstLogin ? mViewPagers[i] : mLoadingPager); pagers.add(imageView_viewpager); } pagers.get(viewpagersize - 1).setOnClickListener(Loading_Pager.this); viewpager.setAdapter(new ViewPagerAdapter(pagers)); viewpager.setOnPageChangeListener(this); } /** * 初始化圆点布局 * * @param linearlayout */ private void initLinearLayout(LinearLayout linearlayout) { ImageView imageView_point = null; linearlayout.setVisibility(mIsFirstLogin ? View.VISIBLE : View.GONE); LinearLayout.LayoutParams params_point = new LinearLayout.LayoutParams( mPointWidth, mPointHeight); params_point.leftMargin = mPointLeftMargin; params_point.rightMargin = mPointRightMargin; for (int i = 0; i < mViewPagerSize; i++) { // 圆点添加 imageView_point = new ImageView(mContext); imageView_point.setBackgroundResource(mPageIndicatorNormal); linearlayout.addView(imageView_point, params_point); } linearlayout.getChildAt(0).setBackgroundResource(mPageIndicatorFocused); } /** * viewpager适配器实现PagerAdapter至少实现一下四个方法 * * instantiateItem(ViewGroup, int):初始化界面 * * destroyItem(ViewGroup, int, Object):销毁viewitem * * getCount():获得ViewPager的数量 * * isViewFromObject(View, Object) */ class ViewPagerAdapter extends PagerAdapter { private ArrayList<View> mViews = null; public ViewPagerAdapter(ArrayList<View> views) { // TODO Auto-generated constructor stub this.mViews = views; } // 销毁position位置的界面 @Override public void destroyItem(View v, int position, Object arg2) { // TODO Auto-generated method stub ((ViewPager) v).removeView(mViews.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(mViews.get(position)); return mViews.get(position); } @Override public int getCount() { // TODO Auto-generated method stub return mViews.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub for (int i = 0; i < mViewPagerSize; i++) { if (i == arg0) { mLinearLayout.getChildAt(i).setBackgroundResource( mPageIndicatorFocused); } else { mLinearLayout.getChildAt(i).setBackgroundResource( mPageIndicatorNormal); } } } @Override public void onClick(View v) { mHandler.sendEmptyMessage(HANDLERID_CLICK); } }
使用代码:
package com.xhm.meishi; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.FragmentActivity; /** * 实现判断是否显示新功能展示页,还是应用加载页; * * 功能展示页:功能展示图片res集合,最后一页点击进入加载页(不直接进入应用首页,应为在加载页时加载一些数据); * * 应用加载页:实现一些数据的验证和加载等(独立的线程实现); * * @author Administrator * */ public class Activity_Loading_Pager extends FragmentActivity { /** * 以下变量需要自定义初始化 */ // viewpager中圆点资源 private int mPageIndicatorNormal = R.drawable.page_indicator; private int mPageIndicatorFocused = R.drawable.page_indicator_focused; // viewpager图片资源数组资源以及数量 private int mViewPagers[] = { R.drawable.load_page_0, R.drawable.load_page_1, R.drawable.load_page_2 }; // 加载页图片资源 private int mLoadingPager = R.drawable.a; private Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); mHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); Intent intent = null; switch (msg.what) { case Loading_Pager.HANDLERID_CLICK: intent = new Intent(Activity_Loading_Pager.this, Activity_Home.class); startActivity(intent); break; case Loading_Pager.HANDLERID_TIME: intent = new Intent(Activity_Loading_Pager.this, Activity_Home.class); startActivity(intent); break; default: break; } finish(); } }; setContentView(new Loading_Pager(this, mViewPagers, mLoadingPager, mPageIndicatorNormal, mPageIndicatorFocused, mHandler, 3000) .getView()); } }
源码及JAR下载:http://download.csdn.net/detail/as294985925/6594547