Android应用程序欢迎界面的实现,代码简单,很好理解,延时指定时间后再进入下一界面。(ActivitySupport是继承Activity的base类)
package com.pmmq.lvim.ui;
import com.pmmq.lvim.R;
import com.pmmq.lvim.config.Constant;
import com.pmmq.lvim.config.LoginConfig;
import android.os.Bundle;
import android.os.Handler;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
/**
* WelcomeActivity
* @author shark
* App欢迎页面
* 20140426
*/
public class WelcomeActivity extends ActivitySupport {
public Context mContext;
private LoginConfig loginConfig;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_welcome);
loginConfig = getLoginConfig();
mContext = WelcomeActivity.this;
if (loginConfig.isFirstStart()) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/**
* 程序第一次使用进入Guide页面
*/
Intent intent = new Intent();
intent.setClass(context, GuideActivity.class);
startActivity(intent);
finish();
}
}, Constant.TIME_DELAY_GUIDE);
} else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/**
* 程序不是第一次使用进入程序
*/
Intent intent = new Intent();
intent.setClass(context, LoginActivity.class);
startActivity(intent);
finish();
}
}, Constant.TIME_DELAY_WELCOME);
}
}
}
进入下一界面前会判断是否是第一次进入该程序,如果是第一次进入,则跳转到引导界面GuideActivity。
package com.pmmq.lvim.ui;
import java.util.ArrayList;
import com.pmmq.lvim.R;
import com.pmmq.lvim.adapter.GuidePagerAdapter;
import android.os.Bundle;
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;
import android.widget.LinearLayout;
/**
* GuideActivity
* @author shark
* App引导页面
* 20140426
*/
public class GuideActivity extends ActivitySupport {
// 翻页控件
private ViewPager mViewPager;
private ImageView mPage0;
private ImageView mPage1;
private ImageView mPage2;
private ImageView mPage3;
private ImageView mPage4;
private LinearLayout linearLayout_indicator;
@Override
public void onCreate(Bundle savedInstanceState) {
// 去掉标题栏全屏显示
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
mViewPager = (ViewPager) findViewById(R.id.whatsnew_viewpager);
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);
linearLayout_indicator = (LinearLayout) this.findViewById(R.id.layout_indicator);
/*
* 这里是每一页要显示的布局,根据应用需要和特点自由设计显示的内容 以及需要显示多少页等
*/
LayoutInflater mLi = LayoutInflater.from(this);
View view1 = mLi.inflate(R.layout.whats_news_gallery1, null);
View view2 = mLi.inflate(R.layout.whats_news_gallery2, null);
View view3 = mLi.inflate(R.layout.whats_news_gallery3, null);
View view4 = mLi.inflate(R.layout.whats_news_gallery4, null);
View view5 = mLi.inflate(R.layout.whats_news_gallery5, 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);
// 填充ViewPager的数据适配器
GuidePagerAdapter mPagerAdapter = new GuidePagerAdapter(views,getApplicationContext(), GuideActivity.this);
mViewPager.setAdapter(mPagerAdapter);
}
public class MyOnPageChangeListener implements OnPageChangeListener {
public void onPageSelected(int page) {
// 翻页时当前page,改变当前状态园点图片
switch (page) {
case 0:
mPage0.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_selected));
mPage1.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_unselected));
break;
case 1:
mPage1.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_selected));
mPage0.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_unselected));
mPage2.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_unselected));
break;
case 2:
mPage2.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_selected));
mPage1.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_unselected));
mPage3.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_unselected));
break;
case 3:
linearLayout_indicator.setVisibility(View.VISIBLE);
mPage3.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_selected));
mPage2.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_unselected));
mPage4.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_unselected));
break;
case 4:
linearLayout_indicator.setVisibility(View.INVISIBLE);
mPage3.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_unselected));
mPage4.setImageDrawable(getResources().getDrawable(
R.drawable.moon_page_selected));
break;
}
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
}
}
}
activity_guide.xml
<?xml version="1.0" encoding="utf-8"?>
<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.ViewPager>
<LinearLayout
android:id="@+id/layout_indicator"
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="@drawable/moon_page_selected" />
<ImageView
android:id="@+id/page1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:scaleType="matrix"
android:src="@drawable/moon_page_unselected" />
<ImageView
android:id="@+id/page2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:scaleType="matrix"
android:src="@drawable/moon_page_unselected" />
<ImageView
android:id="@+id/page3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:scaleType="matrix"
android:src="@drawable/moon_page_unselected" />
<ImageView
android:id="@+id/page4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:scaleType="matrix"
android:src="@drawable/moon_page_unselected" />
</LinearLayout>
</FrameLayout>
GuidePagerAdapter.java :
package com.pmmq.lvim.adapter;
import java.util.ArrayList;
import com.pmmq.lvim.R;
import com.pmmq.lvim.ui.LoginActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* GuidePagerAdapter
* @author shark
* 20140426
*/
public class GuidePagerAdapter extends PagerAdapter {
private ArrayList<View> mViews;
// private Context mContext;
private Activity mActivity;
public GuidePagerAdapter(ArrayList<View> views) {
this.mViews = views;
}
public GuidePagerAdapter(ArrayList<View> views, Context context, Activity activity) {
this.mViews = views;
// this.mContext = context;
this.mActivity = activity;
}
@Override
public int getCount() {
if (mViews != null) {
return mViews.size();
}
return 0;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView(mViews.get(position));
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public Object instantiateItem(View container, int position) {
((ViewPager) container).addView(mViews.get(position));
if (position == mViews.size() - 1) {
Button button = (Button) container
.findViewById(R.id.what_news_start_button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
goMain();
}
});
}
return mViews.get(position);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
private void goMain() {
// 跳转到程序登录界面
Intent intent = new Intent(mActivity, LoginActivity.class);
mActivity.startActivity(intent);
mActivity.finish();
}
}
其中whats_news_gallery1、whats_news_gallery2、whats_news_gallery3、whats_news_gallery4类似,welcome_page_1是背景图片,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/welcome_page_1"
android:orientation="vertical" >
</LinearLayout>
whats_news_gallery5.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/welcome_page_5"
android:orientation="vertical" >
<Button
android:id="@+id/what_news_start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/whats_new_start_btn"
android:text="@string/start" />
</RelativeLayout>
最后一页有一个Button,点击该Button进入下一界面。