一般的app第一次安装启动的时候,都会有一个启动页面和引导页的画面,然后才进入主程序。anndroid中的ViewPagerAdapter 是一个继承与PageAdapter的 页面引导适配器。由于我的笔记本太卡,AndroidStudio hold不住,装X不成功,狠心卸载了,买新电脑之前还是用eclipse顶着。最近也写了个demo试了一下,觉得还可以。
1.新建一个项目,结构如图:
初始页面splash.xml
<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=".SplashActivity" > <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="@drawable/welcome_android"
android:scaleType="centerCrop" /> <TextView
android:id="@+id/txtTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:layout_marginTop="21dp"
android:textSize="18dp"
android:text="3秒" /> </RelativeLayout>
SplashActivity.java后台代码
package com.wym.dxdmallandroid; import java.util.Timer;
import java.util.TimerTask; import com.dxd.dxdmallandroid.R; import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView; /**
* Author:WYM
*/
public class SplashActivity extends Activity {
boolean isFirstIn = false; private static final int GO_HOME = 1000;
private static final int GO_GUIDE = 1001;
private static final long SPLASH_DELAY_MILLIS = 3000;// 延迟3秒
private static final String SHAREDPREFERENCES_NAME = "first_pref";//存取程序是否是第一次启动
private TextView txtTime;
private int recLen = 3;
Timer timer = new Timer();
/**
* Handler:跳转到不同界面
*/
private Handler mHandler = new Handler() { @Override
public void handleMessage(Message msg) {
switch (msg.what) {
case GO_HOME:
goHome();
break;
case GO_GUIDE:
goGuide();
break;
}
super.handleMessage(msg);
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
txtTime=(TextView) findViewById(R.id.txtTime);
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() { //ui thread
@Override
public void run() {
txtTime.setText(recLen+"秒");
recLen--;
if(recLen<-1)
{
timer.cancel();
txtTime.setVisibility(View.GONE);
}
}
});
}
};
timer.schedule(task, 1000, 1000); // timeTask
init();
} private void init() { // 读取SharedPreferences中需要的数据
// 使用SharedPreferences来记录程序的使用次数
SharedPreferences preferences = getSharedPreferences(
SHAREDPREFERENCES_NAME, MODE_PRIVATE); // 取得相应的值,如果没有该值,说明还未写入,用true作为默认值
isFirstIn = preferences.getBoolean("isFirstIn", true); // 判断程序与第几次运行,如果是第一次运行则跳转到引导界面,否则跳转到主界面
if (!isFirstIn) {
// 使用Handler的postDelayed方法,3秒后执行跳转到MainActivity
mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
} else {
mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
} } //跳转到主页
private void goHome() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
} //跳转到初始页
private void goGuide() {
Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
} }
2.简单的OnPageChangeListener运用,ViewPagerAdapter的layout文件和java代码。viewpager是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。ViewPager的适配器一般有两种,传入List<View> views 对应的adapter就是ViewPagerAdapter,如果传的是fragment[] framgments的话就是FragmentPagerAdapter或者FragmentStatePagerAdapter。我传的就是List<View>,所以用ViewPagerAdapter
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <!-- <LinearLayout
android:id="@+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24.0dp"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:padding="15.0dip"
android:src="@drawable/dot" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:padding="15.0dip"
android:src="@drawable/dot" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:padding="15.0dip"
android:src="@drawable/dot" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:padding="15.0dip"
android:src="@drawable/dot" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:padding="15.0dip"
android:src="@drawable/dot" />
</LinearLayout> --> </RelativeLayout>
package com.wym.dxdmallandroid; import java.util.ArrayList;
import java.util.List; import com.dxd.dxdmallandroid.R;
import com.wym.splansh.adapter.ViewPagerAdapter; import android.app.Activity;
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; /**
* @Version 1.0
*
*
*/
public class GuideActivity extends Activity implements OnPageChangeListener { private ViewPager vp;
private ViewPagerAdapter vpAdapter;
private List<View> views; // 底部小点图片
//private ImageView[] dots; // 记录当前选中位置
private int currentIndex; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide);
// 初始化页面
initViews();
// 初始化底部小点
//initDots();
} @SuppressWarnings("deprecation")
private void initViews() {
LayoutInflater inflater = LayoutInflater.from(this); views = new ArrayList<View>();
// 初始化引导图片列表
views.add(inflater.inflate(R.layout.what_new_one, null));
views.add(inflater.inflate(R.layout.what_new_two, null));
views.add(inflater.inflate(R.layout.what_new_three, null));
views.add(inflater.inflate(R.layout.what_new_four, null));
views.add(inflater.inflate(R.layout.what_new_five, null));
// 初始化Adapter
vpAdapter = new ViewPagerAdapter(views, this); vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(vpAdapter);
// 绑定回调
vp.setOnPageChangeListener(this);
} // private void initDots() {
// LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
//
// dots = new ImageView[views.size()];
//
// // 循环取得小点图片
// for (int i = 0; i < views.size(); i++) {
// dots[i] = (ImageView) ll.getChildAt(i);
// dots[i].setEnabled(true);// 都设为灰色
// }
//
// currentIndex = 0;
// dots[currentIndex].setEnabled(false);// 设置为白色,即选中状态
// } private void setCurrentDot(int position) {
if (position < 0 || position > views.size() - 1
|| currentIndex == position) {
return;
} // dots[position].setEnabled(false);
// dots[currentIndex].setEnabled(true); currentIndex = position;
} // 当滑动状态改变时调用
@Override
public void onPageScrollStateChanged(int arg0) {
} // 当当前页面被滑动时调用
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
} // 当新的页面被选中时调用
@Override
public void onPageSelected(int arg0) {
// 设置底部小点选中状态
setCurrentDot(arg0);
} }