前言:
- 启动app先进入闪屏界面,接下来跳转到viewpager引导界面。。。。
整体结构就是 这样:
- 布局文件
(splash.xml)直接一个背景
(view_1 view_2 view_3.xml)直接通过ImageView引用一张图片
(welcome.xml)TextView一句话
(gudide.xml)
<?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.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/iv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/login_point_selected" />
<ImageView
android:id="@+id/iv_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/login_point" />
<ImageView
android:id="@+id/iv_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/login_point" />
</LinearLayout>
</RelativeLayout>
- Splash.java(闪屏界面)
public class SplashActivity extends Activity {
private Thread newThread;
private long threadtime = 3000;
private boolean ispause = false;
private boolean isSpinshActivity = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置图片格式
setondraweble();
setContentView(R.layout.splash);
//設置睡眠時間
setSpinshTime();
}
private void setSpinshTime() {
newThread = new Thread(){
@Override
public void run() {
super.run();
int ms = 0;
try {
while(isSpinshActivity&&ms<threadtime){
sleep(100);
if(!ispause){
ms+=100;
}
}
//设置跳转
Intent i = new Intent(SplashActivity.this,GuideActivity.class);
startActivity(i);
finish();
}catch (InterruptedException e) {
e.printStackTrace();
}
}
};
newThread.start();
}
//設置全屏
private void setondraweble() {
// 设置无标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 设置全屏参数
int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
// 获得window
Window myWindow = this.getWindow();
// 输出
myWindow.setFlags(flag, flag);
}
// 暂停界面
@Override
protected void onPause() {
super.onPause();
ispause = true;
}
// 又回到这个界面
@Override
protected void onResume() {
super.onResume();
ispause = false;
}
}
- GuideActivity.java(viewpager引导界面)
public class GuideActivity extends Activity implements OnClickListener,OnPageChangeListener{
private ViewPager viewpager;
private List<View> views;
private MyPageAdapter vpadapter;
private Button join;
private ImageView[] dots;
private int viewid[] = {R.id.iv_1,R.id.iv_2,R.id.iv_3};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide);
//我们先要得到view对象
iviview();
//接下来图片初始化
inidots();
}
//初始化更为方便
private void inidots() {
dots = new ImageView[views.size()];
for(int i=0;i<views.size();i++){
dots[i] = (ImageView) findViewById(viewid[i]);
}
}
@SuppressLint("InflateParams") @SuppressWarnings("static-access")
private void iviview() {
//获得view对象
LayoutInflater lf = getLayoutInflater().from(GuideActivity.this);
View view1 = lf.inflate(R.layout.view_1, null);
View view2 = lf.inflate(R.layout.view_2, null);
View view3 = lf.inflate(R.layout.view_3, null);
views = new ArrayList<View>();// 将要分页显示的View装入数组中
views .add(view1);
views .add(view2);
views .add(view3);
//调用adapter
vpadapter = new MyPageAdapter(views,this);
//找到viewpager控件
viewpager = (ViewPager) findViewById(R.id.viewpager);
viewpager.setAdapter(vpadapter);
//判断Button的位置
join = (Button)views.get(2).findViewById(R.id.join);
//设置监听事件
join.setOnClickListener(this);
setOnPageChangeListener(this);
}
//按钮点击事件
@Override
public void onClick(View arg0) {
Intent i = new Intent(GuideActivity.this,ViewPagerDemoActivity.class);
startActivity(i);
finish();
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
//选择了这个pager(和小圆点)
@Override
public void onPageSelected(int position) {
for(int i =0;i<views.size();i++){
//滑动到当前页面 ,小圆点变化
if(position==i){
//引用资源
dots[i].setImageResource(R.id.iv_1);
}
else{
dots[i].setImageResource(R.id.iv_2);
}
}
}
}
- MyPageAdapter.java(自定义Adapter继承PagerAdapter)
实现4个方法
public class MyPageAdapter extends PagerAdapter{
private List<View> views;
@SuppressWarnings("unused")
private Context context;
public MyPageAdapter(List<View> views, Context context) {
super();
this.views = views;
this.context = context;
}
// viewpager中的要显示的View的总数量
@Override
public int getCount()
{
return views.size();
}
// 滑动切换的时候销毁当前的View
@Override
public void destroyItem(ViewGroup container, int position,
Object object)
{
((ViewPager) container).removeView(views.get(position));
}
// 每次滑动的时候生成的View
@Override
public Object instantiateItem(ViewGroup container, int position)
{
((ViewPager) container).addView(views.get(position));
return views.get(position);
}
// 官方建议这样写
@Override
public boolean isViewFromObject(View arg0, Object arg1)
{
return arg0 == arg1;
}
}
最后由引导界面进入主Activity界面
注:Activity不要忘记注册
<activity
android:name=".SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ViewPagerDemoActivity"></activity>
<activity android:name=".GuideActivity"></activity>
结果: