1)用户可以点击上面的“开始使用”这样的按钮可以进入主界面;
2)用户接着滑动下一个图片的手势而进入主界面;
3)用户在引导界面点击返回键的时候直接进入主界面;
增加“开始使用”按钮方式:
可以定义一个layout的xml再加载:一个LinearLayout,里面一个button。默认button是“gone”,
xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/guide_item" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="5" /> <Button android:id="@+id/start" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/guide_start" android:visibility="gone" > </Button> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout>
button上下增加了textview,我是为了控制button在整个界面的位置。
引导的图片是其背景即可。
实现如下:
guide_activity.xml
<?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.support.v4.view.ViewPager android:id="@+id/guide_view" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
GuideActivity.java:
/** * 引导界面 * @author maria * 2012-07-19 */ package com.maria.test; import java.util.ArrayList; import java.util.List; import com.trunkbow.talker.R; import android.app.Activity; import android.content.Context; 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.DisplayMetrics; import android.view.GestureDetector; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; public class GuideActivity extends Activity { private ViewPager viewPager; private List<View> mImageViews; // 滑动的图片集合 private int[] imageResId; // 图片ID private int currentItem = 0; // 当前图片的索引号 private GestureDetector gestureDetector; // 用户滑动 /** 记录当前分页ID */ private int flaggingWidth;// 互动翻页所需滚动的长度是当前屏幕宽度的1/3 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.guide_activity); gestureDetector = new GestureDetector(new GuideViewTouch()); // 获取分辨率 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); flaggingWidth = dm.widthPixels / 3; imageResId = new int[] { R.drawable.guide_1, R.drawable.guide_2 }; mImageViews = new ArrayList<View>(); // 初始化图片资源 LayoutInflater viewInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); // 0 View convertView0 = viewInflater.inflate(R.layout.guide_item, null); LinearLayout linearLayout0 = (LinearLayout) convertView0 .findViewById(R.id.guide_item); linearLayout0.setBackgroundResource(imageResId[0]); mImageViews.add(linearLayout0); // 1 View convertView1 = viewInflater.inflate(R.layout.guide_item, null); LinearLayout linearLayout1 = (LinearLayout) convertView1 .findViewById(R.id.guide_item); linearLayout1.setBackgroundResource(imageResId[1]); Button btn = (Button) convertView1.findViewById(R.id.start); btn.setVisibility(View.VISIBLE); btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub GoToMainActivity(); } }); mImageViews.add(linearLayout1); viewPager = (ViewPager) findViewById(R.id.guide_view); viewPager.setAdapter(new MyAdapter());// 设置填充ViewPager页面的适配器 // 设置一个监听器,当ViewPager中的页面改变时调用 viewPager.setOnPageChangeListener(new MyPageChangeListener()); } @Override public boolean dispatchTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { event.setAction(MotionEvent.ACTION_CANCEL); } return super.dispatchTouchEvent(event); } private class GuideViewTouch extends SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (currentItem == 1) { if (Math.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY() - e2.getY()) && (e1.getX() - e2.getX() <= (-flaggingWidth) || e1 .getX() - e2.getX() >= flaggingWidth)) { if (e1.getX() - e2.getX() >= flaggingWidth) { GoToMainActivity(); return true; } } } return false; } } /** * 进入主界面 */ void GoToMainActivity() { Intent i = new Intent(GuideActivity.this, MainActivivty.class); startActivity(i); finish(); } /** * 当ViewPager中页面的状态发生改变时调用 * * @author Administrator * */ private class MyPageChangeListener implements OnPageChangeListener { /** * This method will be invoked when a new page becomes selected. * position: Position index of the new selected page. */ public void onPageSelected(int position) { currentItem = position; } public void onPageScrollStateChanged(int arg0) { } public void onPageScrolled(int arg0, float arg1, int arg2) { } } /** * 填充ViewPager页面的适配器 * * @author Administrator * */ private class MyAdapter extends PagerAdapter { @Override public int getCount() { return imageResId.length; } @Override public Object instantiateItem(View arg0, int arg1) { ((ViewPager) arg0).addView(mImageViews.get(arg1)); return mImageViews.get(arg1); } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView((View) arg2); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { } @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View arg0) { } @Override public void finishUpdate(View arg0) { } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK) { GoToMainActivity(); return false; } return super.onKeyDown(keyCode, event); } }
本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/1230805,如需转载请自行联系原作者