很多手机软件在安装后首次运行都会进入到引导页面,再次运行时会进入到主页面。
多了不说了,先看效果图:
代码如下:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/mainRLayout"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#000000" ><!-- 自定义滑动控件 --><com.ericssonlabs.ScrollLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/ScrollLayout"android:layout_width="fill_parent"android:layout_height="fill_parent"android:visibility="visible" ><!-- 每一页的布局均以一个RelativeLayout来控制,后面类似,这里一共四个 --><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@drawable/w01" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="90dp"android:text="微信,不只是个聊天工具"android:textColor="#FFFFFF"android:textSize="18sp" /></RelativeLayout><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@drawable/w02" ><TextViewandroid:id="@+id/t1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="96dp"android:gravity="center_horizontal"android:text="第一次,你可以使用透明背景的动画表情,来表达你此刻的心情"android:textColor="#FFFFFF"android:textSize="18sp" /></RelativeLayout><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@drawable/w03" /><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@drawable/w01" ><!-- 点击该按钮后就进入OtherActivit了 --><Buttonandroid:id="@+id/startBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_gravity="center_vertical"android:layout_marginBottom="90dp"android:layout_marginLeft="8dp"android:layout_marginRight="8dp"android:background="@drawable/button_bg"android:text="开始我的微信生活"android:textColor="#FFFFFF"android:textSize="18sp" /></RelativeLayout></com.ericssonlabs.ScrollLayout><!-- 这个布局是下面显示的小圆点的布局,其中ImageView的数量要与上面RelativeLayout的数量对应 --><LinearLayoutandroid:id="@+id/llayout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="25dp"android:orientation="horizontal"android:visibility="visible" ><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:clickable="true"android:padding="5dp"android:src="@drawable/page_indicator_bg" /><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:clickable="true"android:padding="5dp"android:src="@drawable/page_indicator_bg" /><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:clickable="true"android:padding="5dp"android:src="@drawable/page_indicator_bg" /><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:clickable="true"android:padding="5dp"android:src="@drawable/page_indicator_bg" /></LinearLayout><!-- 这个布局是最后点击按钮后启动新界面的一个动画效果 --><LinearLayoutandroid:id="@+id/animLayout"android:layout_width="fill_parent"android:layout_height="fill_parent"android:visibility="gone" ><LinearLayoutandroid:id="@+id/leftLayout"android:layout_width="wrap_content"android:layout_height="fill_parent" ><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/whatsnew_left" /><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/whatsnew_left_m" /></LinearLayout><LinearLayoutandroid:id="@+id/rightLayout"android:layout_width="fill_parent"android:layout_height="fill_parent" ><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/whatsnew_right_m" /><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/whatsnew_right" /></LinearLayout></LinearLayout></RelativeLayout>
orther.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:gravity="center"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="30sp"android:textStyle="bold"android:text="开始新的旅程吧!!!" /></LinearLayout>
下面是java代码
ScrollLayout.java
package com.ericssonlabs;import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;public class ScrollLayout extends ViewGroup {private static final String TAG = "ScrollLayout";private VelocityTracker mVelocityTracker; // private static final int SNAP_VELOCITY = 600;private Scroller mScroller;private int mCurScreen;private int mDefaultScreen = 0;private float mLastMotionX;private OnViewChangeListener mOnViewChangeListener;public ScrollLayout(Context context) {super(context);init(context);}public ScrollLayout(Context context, AttributeSet attrs) {super(context, attrs);init(context);}public ScrollLayout(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init(context);}private void init(Context context) {mCurScreen = mDefaultScreen;mScroller = new Scroller(context);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {if (changed) {int childLeft = 0;final int childCount = getChildCount();for (int i = 0; i < childCount; i++) {final View childView = getChildAt(i);if (childView.getVisibility() != View.GONE) {final int childWidth = childView.getMeasuredWidth();childView.layout(childLeft, 0, childLeft + childWidth,childView.getMeasuredHeight());childLeft += childWidth;}}}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);final int width = MeasureSpec.getSize(widthMeasureSpec);final int count = getChildCount();for (int i = 0; i < count; i++) {getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);}scrollTo(mCurScreen * width, 0);}public void snapToDestination() {final int screenWidth = getWidth();final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth;snapToScreen(destScreen);}public void snapToScreen(int whichScreen) {// get the valid layout pagewhichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));if (getScrollX() != (whichScreen * getWidth())) {final int delta = whichScreen * getWidth() - getScrollX();mScroller.startScroll(getScrollX(), 0, delta, 0,Math.abs(delta) * 2);mCurScreen = whichScreen;invalidate(); // Redraw the layoutif (mOnViewChangeListener != null) {mOnViewChangeListener.OnViewChange(mCurScreen);}}}@Overridepublic void computeScroll() {if (mScroller.computeScrollOffset()) {scrollTo(mScroller.getCurrX(), mScroller.getCurrY());postInvalidate();}}@Overridepublic boolean onTouchEvent(MotionEvent event) {final int action = event.getAction();final float x = event.getX();switch (action) {case MotionEvent.ACTION_DOWN:if (mVelocityTracker == null) {mVelocityTracker = VelocityTracker.obtain();mVelocityTracker.addMovement(event);}if (!mScroller.isFinished()) {mScroller.abortAnimation();}mLastMotionX = x;break;case MotionEvent.ACTION_MOVE:int deltaX = (int) (mLastMotionX - x);if (IsCanMove(deltaX)) {if (mVelocityTracker != null) {mVelocityTracker.addMovement(event);}mLastMotionX = x;scrollBy(deltaX, 0);}break;case MotionEvent.ACTION_UP:int velocityX = 0;if (mVelocityTracker != null) {mVelocityTracker.addMovement(event);mVelocityTracker.computeCurrentVelocity(1000);velocityX = (int) mVelocityTracker.getXVelocity();}if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {// Fling enough to move leftLog.e(TAG, "snap left");snapToScreen(mCurScreen - 1);} else if (velocityX < -SNAP_VELOCITY&& mCurScreen < getChildCount() - 1) {// Fling enough to move rightLog.e(TAG, "snap right");snapToScreen(mCurScreen + 1);} else {snapToDestination();}if (mVelocityTracker != null) {mVelocityTracker.recycle();mVelocityTracker = null;}break;}return true;}private boolean IsCanMove(int deltaX) {if (getScrollX() <= 0 && deltaX < 0) {return false;}if (getScrollX() >= (getChildCount() - 1) * getWidth() && deltaX > 0) {return false;}return true;}public void SetOnViewChangeListener(OnViewChangeListener listener) {mOnViewChangeListener = listener;}}
ScrollLayoutActivity.java
package com.ericssonlabs;import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;public class ScrollLayoutActivity extends Activity implements OnViewChangeListener{private ScrollLayout mScrollLayout;private ImageView[] imgs;private int count;private int currentItem;private Button startBtn;private RelativeLayout mainRLayout;private LinearLayout pointLLayout;private LinearLayout leftLayout;private LinearLayout rightLayout;private LinearLayout animLayout;//记录程序的使用次数private SharedPreferences preferences;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);preferences = getSharedPreferences("count",MODE_WORLD_READABLE);//记录软件运行次数int count = preferences.getInt("count", 0);Editor editor = preferences.edit();editor.putInt("count", count+1);//提交修改editor.commit();//判断程序是否是首次运行if(count==0){initView();}else{//非首次运行,跳转到欢迎界面Intent intent = new Intent();intent.setClass(ScrollLayoutActivity.this, OtherActivity.class);startActivity(intent);ScrollLayoutActivity.this.finish();}}/** 初始化*/private void initView() {mScrollLayout = (ScrollLayout) findViewById(R.id.ScrollLayout);pointLLayout = (LinearLayout) findViewById(R.id.llayout);mainRLayout = (RelativeLayout) findViewById(R.id.mainRLayout);startBtn = (Button) findViewById(R.id.startBtn);startBtn.setOnClickListener(onClick);animLayout = (LinearLayout) findViewById(R.id.animLayout);leftLayout = (LinearLayout) findViewById(R.id.leftLayout);rightLayout = (LinearLayout) findViewById(R.id.rightLayout);count = mScrollLayout.getChildCount();imgs = new ImageView[count];for (int i = 0; i < count; i++) {imgs[i] = (ImageView) pointLLayout.getChildAt(i);imgs[i].setEnabled(true);imgs[i].setTag(i);}currentItem = 0;imgs[currentItem].setEnabled(false);mScrollLayout.SetOnViewChangeListener(this);}private View.OnClickListener onClick = new View.OnClickListener() {@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.startBtn:mScrollLayout.setVisibility(View.GONE);pointLLayout.setVisibility(View.GONE);animLayout.setVisibility(View.VISIBLE);mainRLayout.setBackgroundResource(R.drawable.whatsnew_bg);Animation leftOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate_left);Animation rightOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate_right);leftLayout.setAnimation(leftOutAnimation);rightLayout.setAnimation(rightOutAnimation);leftOutAnimation.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {mainRLayout.setBackgroundColor(Color.BLACK);}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {leftLayout.setVisibility(View.GONE);rightLayout.setVisibility(View.GONE);Intent intent = new Intent(ScrollLayoutActivity.this,OtherActivity.class);ScrollLayoutActivity.this.startActivity(intent);ScrollLayoutActivity.this.finish(); overridePendingTransition(R.anim.zoom_out_enter,R.anim.zoom_out_exit);}});break;}}};@Overridepublic void OnViewChange(int position) {setcurrentPoint(position);}private void setcurrentPoint(int position) {if (position < 0 || position > count - 1 || currentItem == position) {return;}imgs[currentItem].setEnabled(true);imgs[position].setEnabled(false);currentItem = position;}
}
OnViewChangeListener.java
package com.ericssonlabs;public interface OnViewChangeListener {public void OnViewChange(int view);
}
OtherActivity.java
package com.ericssonlabs;import android.app.Activity;
import android.os.Bundle;public class OtherActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.other);}}
preferences = getSharedPreferences("count",MODE_WORLD_READABLE);//记录软件运行次数int count = preferences.getInt("count", 0);Editor editor = preferences.edit();editor.putInt("count", count+1);//提交修改editor.commit();//判断程序是否是首次运行if(count==0){initView();}else{//非首次运行,跳转到欢迎界面Intent intent = new Intent();intent.setClass(ScrollLayoutActivity.this, OtherActivity.class);startActivity(intent);ScrollLayoutActivity.this.finish();}
源码下载地址: http://download.csdn.net/detail/wangzhongshun/6314305