Android开发全程记录(一)——Android客户端启动页实现

1.Android客户端安装后
  第一次启动:启动页——引导页(功能简介页)——首页
     以后启动:启动页——首页
  因此这里要在客户端启动的时候做判断,这里使用 SharedPreferences 保存状态,当的值为true时,表示是第一次启动,要显示引导页,否则直接显示首页。
  保存和获取启动标识的代码如下:
package com.jason.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/*
 * 基本功能:存储和访问SharedPreferences
 * 创建:Jason
 */
public class OperatingSharedPreferences {
	/**
	 * <pre>
	 * 基本功能:保存启动标识到SharedPreferences
	 * 编写:Jason
	 * @param context
	 * @param opentimes
	 * </pre>
	 */
	public static void setSharedPreferences(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				"whtopentimes", Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();// 获取编辑器
		editor.putBoolean("firstopen", false);
		editor.commit();// 提交修改
	}

	/**
	 * <pre>
	 * 基本功能:取得SharedPreferences中存储的启动标识
	 * 编写:Jason
	 * @param context
	 * @return
	 * </pre>
	 */
	public static boolean getSharedPreferences(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				"whtopentimes", Context.MODE_PRIVATE);
		// getString()第二个参数为缺省值,如果preference中不存在该key,将返回缺省值true
		boolean firstopen = sharedPreferences.getBoolean("firstopen", true);
		return firstopen;
	}
}

注:Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下。
2.有了获取启动标识的方法后只需要在启动页做一个简单的判断,代码如下:
package com.jason.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;

import com.jason.util.OperatingSharedPreferences;

/*
 * 
 * 基本功能:开机动画
 * 创建:Jason
 *
 */
public class BootActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_boot);
		// 通过handler 延时2秒 执行r任务
		new Handler().postDelayed(new LoadMainTabTask(), 2000);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.boot, menu);
		return true;
	}

	/**
	 * <pre>
	 * 基本功能:加载介绍界面或主页面
	 * 编写:Jason
	 * </pre>
	 */
	private class LoadMainTabTask implements Runnable {
		public void run() {
			boolean opentimes = OperatingSharedPreferences
					.getSharedPreferences(getApplicationContext());
			// 若为新安装的应用,进入介绍界面,并保存启动次数到SharedPreferences。若不是新安装,则直接进入首页
			if (opentimes) {
				OperatingSharedPreferences
						.setSharedPreferences(getApplicationContext());
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(),
						PowerSplashActivity.class);
				startActivity(intent);//打开引导页
			} else {
				Intent intent = new Intent(getApplicationContext(),
						MainActivity.class);
				startActivity(intent);//直接打开首页
			}
			finish();
		}
	}
}
3.直接打开首页就不用多说了。接下来就是实现引导页PowerSplashActivity.java,像墨迹天气一样,图片左右滑动,然后进入首页。
PowerSplashActivity.java就是实现图片左右循环滑动的效果,这里使用网上的资源(ViewPager实现左右循环滑动,原文地址:
首先定义 PowerSplashActivity.java的布局,布局文件如下:
<?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/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/viewGroup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="30dp"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >
        </LinearLayout>
    </RelativeLayout>

</FrameLayout>

下面是Java中的实现
package com.jason.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
/*
 * 基本功能:展示开机引导页
 * 创建:Jason
 */
public class PowerSplashActivity extends Activity {
	ViewPager viewPager;
	ArrayList<View> list;
	ViewGroup main, group;
	ImageView imageView;
	ImageView[] imageViews;
	Button enter;
	private static int c_id = 0;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		LayoutInflater inflater = getLayoutInflater();
		View layout1 = inflater.inflate(R.layout.itemone,null); 
		View layout2 = inflater.inflate(R.layout.itemtwo,null); 
		View layout3 = inflater.inflate(R.layout.itemthree,null); 
		View layout4 = inflater.inflate(R.layout.itemfour,null); 
		View layout5 = inflater.inflate(R.layout.itemfive,null); 
		enter = (Button)layout5.findViewById(R.id.enter);
		enter.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(PowerSplashActivity.this,MainActivity.class);
				startActivity(intent);
				finish();
			}
		});
		list = new ArrayList<View>();
		list.add(layout1);
		list.add(layout2);
		list.add(layout3);
		list.add(layout4);
		list.add(layout5);
		imageViews = new ImageView[list.size()];
		ViewGroup main = (ViewGroup) inflater.inflate(R.layout.activity_power_splash, null);
		ViewGroup group = (ViewGroup) main.findViewById(R.id.viewGroup);
		viewPager = (ViewPager) main.findViewById(R.id.viewPager);
		for (int i = 0; i < list.size(); i++) {
			imageView = new ImageView(PowerSplashActivity.this);
			imageView.setLayoutParams(new LayoutParams(12,12));
			//imageView.setPadding(20, 0, 20, 0);
			//设置圆点的间距
			LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(  
			LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
			layout.setMargins(10, 0, 10, 0);  
			imageView.setLayoutParams(layout);  
			imageViews[i] = imageView;
			if (i == 0) {
				imageViews[i].setBackgroundResource(R.drawable.guide_dot_white);
			} else {
				imageViews[i].setBackgroundResource(R.drawable.guide_dot_black);
			}
			group.addView(imageView);
		}
		setContentView(main);
		viewPager.setAdapter(new ImageViewAdapter());
		viewPager.setOnPageChangeListener(new MyListener());
		viewPager.setCurrentItem(300);
		
	}

	class ImageViewAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return Integer.MAX_VALUE;
		}

		@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(list.get(arg1));
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			// TODO Auto-generated method stub
			    try{
					((ViewPager) arg0).addView(list.get(arg1%list.size()),0);
				    }catch (Exception e) {
						// TODO: handle exception
					}
				return list.get(arg1%list.size());
		}

		@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

		}
	}

	class MyListener implements OnPageChangeListener {

		//当滑动状态改变时调用  
		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			//arg0=arg0%list.size();
			
		}

		//当当前页面被滑动时调用  
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		//当新的页面被选中时调用  
		@Override
		public void onPageSelected(int arg0) {
			 if(arg0>2){
				    arg0=arg0%list.size();
				   }
			c_id = arg0;
			for (int i = 0; i < imageViews.length; i++) {
				imageViews[arg0]
						.setBackgroundResource(R.drawable.guide_dot_white);
				if (arg0 != i) {
					imageViews[i]
							.setBackgroundResource(R.drawable.guide_dot_black);
				}
			}
			Log.e("-------------", "当前是第"+c_id+"页");
		}

	}
}

这里引导页一共有5张图片,图片的布局如下:
<?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:orientation="vertical" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/guide02" >
    </ImageView>

</LinearLayout>
前四个页面的布局和上面的一样,只是换了一张背景图片,第五个布局也基本一致,只不过增加了一个点击进入应用的按钮,布局如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/guide05" />

    <Button
        android:id="@+id/enter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="80dip"
        android:layout_gravity="center_horizontal|bottom"
        android:background="#00000000"
        android:textColor="#ffffff"
        android:text="进入应用" 
        android:textSize="20sp"/>
</FrameLayout>
到此Android客户端启动页就基本完成了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值