第一步:创建视图activity_guide.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="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<Button
android:id="@+id/btn_enter_main"
android:layout_width="135dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:background="@drawable/guide_btn_bg"
android:text="@string/start_travel"
android:textColor="@color/guide_btn_text_color"
android:visibility="gone" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp" >
<!-- 点的容器 -->
<LinearLayout
android:id="@+id/guide_container_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
<!-- 添加一个动态的点,和原来的点一样大小 -->
<View
android:id="@+id/guide_focus_point"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/guide_point_focus" />
</RelativeLayout>
</RelativeLayout>
第二步:创建GuideActivity文件
public class GuiderActivity extends Activity{
private ViewPager mViewPager;
private List<ImageView> mImageViews;//图片集合
private Button mBtnStart;//开始按钮
private LinearLayout mPointContainer;//点的容器
private View mFocusPoint;//移动的点
private int mPointSpace;//两点见的距离
private int mScreenWidth;//屏幕宽度
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
initViews();
initDatas();
initEvents();
}
private void initViews() {
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mBtnStart = (Button) findViewById(R.id.btn_enter_main);
mPointContainer = (LinearLayout) findViewById(R.id.guide_container_point);
//移动的点
mFocusPoint = findViewById(R.id.guide_focus_point);
}
private void initDatas() {
mImageViews = new ArrayList<ImageView>();
//先设定一个数组
int[] res = new int[]{R.drawable.guide_1,R.drawable.guide_2, R.drawable.guide_3};
//遍历
for (int i = 0; i < res.length; i++) {
//-设置图片---------------------------------------
ImageView imageView = new ImageView(this);
//把图片资源设定到imageview里
imageView.setImageResource(res[i]);
imageView.setScaleType(ScaleType.FIT_XY);
mImageViews.add(imageView);
//--添加静态的点---------------------------------------
View point = new View(this);
point.setBackgroundResource(R.drawable.guide_point_normal);
//设置点的大小
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(UITools.px2dip(this, 10), UITools.px2dip(this, 10));
//设置间距
if(i!=0)//不是第一个点
params.leftMargin = UITools.px2dip(this, 10);
//添加点的图片
mPointContainer.addView(point,params);
}
mViewPager.setAdapter(new GuiderAdapter());
//---得到屏幕的宽度--------------------------------
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
mScreenWidth = outMetrics.widthPixels;
}
private void initEvents() {
mBtnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//设置已经进入过引导页了
CacheUtil.setBoolean(GuiderActivity.this, WelcomeActivity.IS_FIRST_START, false);
//进入主页
Intent intent = new Intent(GuiderActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
//页面滚动监听事件
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
/**
* 当页面选中的时候
* @param position
*/
@Override
public void onPageSelected(int position) {
//最后一页时
mBtnStart.setVisibility(position == mImageViews.size() - 1 ? View.VISIBLE : View.GONE);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixes) {
//点滑动的原理:在原来的点的容器上添加RelativeLayout ,里面再添加一个点,使其覆盖原来的第一个点,
//移动时,只要设置点的margin-left即可
Logger.i("tag", "position=="+position);
Logger.i("tag", "positionOffset"+positionOffset);
Logger.i("tag", "positionOffsetPixes=="+positionOffsetPixes);
//计算移动时离左边的距离
//mPointSpace*positionOffset移动的偏移量
//position * mPointSpace 几个点之间的距离
int leftMargin = (int) (mPointSpace*positionOffset + position * mPointSpace + 0.5f);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mFocusPoint.getLayoutParams();
params.leftMargin = leftMargin;
mFocusPoint.setLayoutParams(params);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
/**
* 设置点移动时的监听
*/
mPointContainer.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//获得两点见的距离
mPointSpace = mPointContainer.getChildAt(1).getLeft() - mPointContainer.getChildAt(0).getLeft();
}
});
}
}
第三步:创建Adapter文件
class GuiderAdapter extends PagerAdapter{
@Override
public int getCount() {
if (mImageViews != null) {
return mImageViews.size();
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
//展示的imageview
ImageView imageView = mImageViews.get(position);
//添加到容器里
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//从容器里移除
container.removeView((View) object);
}
}
第四部:相关的样式
guide_btn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_red_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/button_red_normal"></item>
</selector>
guide_btn_text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@android:color/black"/>
<item android:color="@android:color/white"/>
</selector>
guide_point_focus.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<corners android:radius="5dp" />
<solid android:color="@color/red" />
</shape>
第五步:相关的工具类
public class UITools {
/**
* 把像素转化为dp
*
* @param context
* @param px
* @return
*/
public static int px2dip(Context context, float px) {
float density = context.getResources().getDisplayMetrics().density;
return (int) (px * density + 0.5f);
}
/**
* 设置窗体的宽度
* @param context
*/
public static void setWinWidth(Activity context) {
DisplayMetrics metric = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metric);
LayoutParams p = context.getWindow().getAttributes();
p.width = (int) (metric.widthPixels * 0.8);
context.getWindow().setAttributes(p);
}
/**
* 设置弹出窗口中listview的高度
*
* @param listView
*/
public static void setListViewHeight(ListView listView) {
// 获取ListView对应的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0); // 计算子项View 的宽高
totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
}
}
CacheUtil.java
public class CacheUtil {
private static final String SP_NAME = "beijin_config";
private static SharedPreferences mPreferences;
private static SharedPreferences getSP(Context context){
if (mPreferences == null) {
mPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
return mPreferences;
}
/**
* 在SharedPreferences通过key 获得boolean类型数据
* @param context
* @param key
* @return
*/
public static boolean getBoolean(Context context , String key){
SharedPreferences sp = getSP(context);
return sp.getBoolean(key, false);
}
/**
* 在SharedPreferences通过key 获得boolean类型数据
* @param context
* @param key
* @param defValue :默认值
* @return
*/
public static boolean getBoolean(Context context , String key, boolean defValue){
SharedPreferences sp = getSP(context);
return sp.getBoolean(key, defValue);
}
/**
* 设置默认值
* @param context
* @param key
* @param value
*/
public static void setBoolean(Context context, String key, boolean value){
SharedPreferences sp = getSP(context);
Editor edit = sp.edit();
edit.putBoolean(key, value);
edit.commit();
}
//----String---------------------------------------------
/**
* 在SharedPreferences通过key 获得String类型数据
* @param context
* @param key
* @return
*/
public static String getString(Context context , String key){
SharedPreferences sp = getSP(context);
return sp.getString(key, null);
}
/**
* 在SharedPreferences通过key 获得String类型数据
* @param context
* @param key
* @param defValue :默认值
* @return
*/
public static String getString(Context context , String key, String defValue){
SharedPreferences sp = getSP(context);
return sp.getString(key, defValue);
}
/**
* 设置默认值
* @param context
* @param key
* @param value
*/
public static void setString(Context context, String key, String value){
SharedPreferences sp = getSP(context);
Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
//----long---------------------------------------------
/**
* 在SharedPreferences通过key 获得Long类型数据
* @param context
* @param key
* @return
*/
public static Long getLong(Context context , String key){
SharedPreferences sp = getSP(context);
return sp.getLong(key, 0);
}
/**
* 在SharedPreferences通过key 获得Long类型数据
* @param context
* @param key
* @param defValue :默认值
* @return
*/
public static Long getLong(Context context , String key, Long defValue){
SharedPreferences sp = getSP(context);
return sp.getLong(key, defValue);
}
/**
* 设置默认值
* @param context
* @param key
* @param value
*/
public static void setLong(Context context, String key, Long value){
SharedPreferences sp = getSP(context);
Editor edit = sp.edit();
edit.putLong(key, value);
edit.commit();
}
//----Integer---------------------------------------------
/**
* 在SharedPreferences通过key 获得Long类型数据
* @param context
* @param key
* @return
*/
public static int getInt(Context context , String key){
SharedPreferences sp = getSP(context);
return sp.getInt(key, 0);
}
/**
* 在SharedPreferences通过key 获得Long类型数据
* @param context
* @param key
* @param defValue :默认值
* @return
*/
public static int getInt(Context context , String key, int defValue){
SharedPreferences sp = getSP(context);
return sp.getInt(key, defValue);
}
/**
* 设置默认值
* @param context
* @param key
* @param value
*/
public static void setInt(Context context, String key, int value){
SharedPreferences sp = getSP(context);
Editor edit = sp.edit();
edit.putInt(key, value);
edit.commit();
}
}
Logger.java
public class Logger {
public static int LOG_LEVEL = 7;
public static final int VERBOSE = 5;
public static final int DEBUG = 4;
public static final int INFO = 3;
public static final int WARN = 2;
public static final int ERROR = 1;
public static void v(String tag, String msg) {
if (LOG_LEVEL > VERBOSE) {
Log.v(tag, msg);
}
}
public static void d(String tag, String msg) {
if (LOG_LEVEL > DEBUG) {
Log.d(tag, msg);
}
}
public static void i(String tag, String msg) {
if (LOG_LEVEL > INFO) {
Log.i(tag, msg);
}
}
public static void w(String tag, String msg) {
if (LOG_LEVEL > WARN) {
Log.w(tag, msg);
}
}
public static void e(String tag, String msg) {
if (LOG_LEVEL > ERROR) {
Log.e(tag, msg);
}
}
}