校园助手APP--启动logo及导航页

不知道为什么昨天发布的一篇博文没有显示出来,草稿箱都没有存档,只好重新写一篇。

介绍启动的logo页面和第一次使用时显示的导航页面。


logo页面的实现是采用新浪微博客户端开发教程里面的方法,一个背景图片,然后动画渐现一个logo图片。

public class LogoActivity extends Activity {
	
	Intent intent = null;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//设置窗口为无标题栏,全屏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		//标记为全屏
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.logo);

		//设置动画
		AlphaAnimation animation = new AlphaAnimation(0.2f, 1.0f);
		animation.setDuration(2000);//2S渐现

		ImageView img_logo = (ImageView) this.findViewById(R.id.img_logo);
		img_logo.setAnimation(animation);//将logo设置为动画显示

		
		UserInfo loginUser = SharedPreferencesUtil.getLoginUser(LogoActivity.this);
		boolean isUsed = SharedPreferencesUtil.getIsUsed(LogoActivity.this);
		
		//判断是否使用过
		if(isUsed){
			//判断是否有自动登录用户
			if(null == loginUser){//无自动登录用户则进入登录界面
				intent = new Intent(LogoActivity.this,LoginActivity.class);
			}else {//有自动登录用户则进入主界面
				intent = new Intent(LogoActivity.this,HomeActivity.class);
			}
		}else {
			intent = new Intent(LogoActivity.this,GuideActivity.class);
		}
//		intent = new Intent(LogoActivity.this, LoginActivity.class);
		
		
		//动画监听
		animation.setAnimationListener(new AnimationListener() {

			//动画结束时跳转
			public void onAnimationEnd(Animation animation) {

				startActivity(intent);
				LogoActivity.this.finish();
				overridePendingTransition(SwitchActivityAnim.rightIn(),SwitchActivityAnim.rightOut());
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
			}
			@Override
			public void onAnimationStart(Animation arg0) {
				startMainService();
			}
		});
		
	}
	
	// 启动主服务
	private void startMainService(){
		Intent service = new Intent(this, MainService.class);
		startService(service);
	}
	
}

界面文件




    


现在先忽略所有的动画,后面会写一篇动画专题。

第一次启动时,logo动画完毕就会进入到导航页面。

导航页面是仿微信的导航页面,代码也是在网上找到的。

public class GuideActivity extends Activity implements OnViewChangeListener{
	
	private MyScrollLayout mScrollLayout;
	private ImageView[] imgs;//标记的点
	private int count;
	private int currentItem;
	private Button startBtn;//最后一页的按钮
	private LinearLayout pointLLayout;//一排点的布局
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide);
        initView();
    }
    
	private void initView() {
		mScrollLayout  = (MyScrollLayout) findViewById(R.id.ScrollLayout);
		pointLLayout = (LinearLayout) findViewById(R.id.llayout);
		startBtn = (Button) findViewById(R.id.startBtn);
		startBtn.setOnClickListener(onClick);
		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() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.startBtn:
				Intent intent = new Intent(GuideActivity.this,LoginActivity.class);
				GuideActivity.this.startActivity(intent);
				GuideActivity.this.finish();
				overridePendingTransition(SwitchActivityAnim.rightIn(),SwitchActivityAnim.rightOut());
				//标记为使用过
				SharedPreferencesUtil.setIsUsed(GuideActivity.this, true);
				break;
			}
		}
	};

	@Override
	public 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;
	}
}

用到的是自定义的容器

public class MyScrollLayout extends ViewGroup {

	private static final String TAG = "ScrollLayout";

	private VelocityTracker mVelocityTracker; // 用于判断甩动手势

	private static final int SNAP_VELOCITY = 600;//尽可能的大,我在其他文章里看到有把这个变量设置为最大的int值

	private Scroller mScroller; // 滑动控制

	private int mCurScreen;//当前页面的index

	private int mDefaultScreen = 0;//默认为第一个页面

	private float mLastMotionX;//上一个触点的x坐标

	private OnViewChangeListener mOnViewChangeListener;//用于更新标记点的

	public MyScrollLayout(Context context) {
		super(context);
		init(context);
	}

	public MyScrollLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public MyScrollLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		init(context);
	}

	private void init(Context context) {
		mCurScreen = mDefaultScreen;

		mScroller = new Scroller(context);
	}

	@Override
	protected 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;
				}
			}
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		final int width = MeasureSpec.getSize(widthMeasureSpec);
//		final int widthMode = MeasureSpec.getMode(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 page
		whichScreen = 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 layout

			if (mOnViewChangeListener != null) {
				mOnViewChangeListener.OnViewChange(mCurScreen);
			}
		}
	}

	@Override
	public void computeScroll() {
		if (mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			postInvalidate();
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		final int action = event.getAction();
		final float x = event.getX();
//		final float y = event.getY();	//主要是涉及到左右滑动

		switch (action) {
		case MotionEvent.ACTION_DOWN:

			Log.i("", "onTouchEvent  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);//滑动距离(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 left
				Log.e(TAG, "snap left");
				snapToScreen(mCurScreen - 1);
			} else if (velocityX < -SNAP_VELOCITY && mCurScreen < getChildCount()-1) {
				// Fling enough to move right
				Log.e(TAG, "snap right");
				snapToScreen(mCurScreen + 1);
			} else {
				snapToDestination();
			}

			if (mVelocityTracker != null) {
				mVelocityTracker.recycle();
				mVelocityTracker = null;
			}
			break;
		}
		return true;
	}

	
	private boolean IsCanMove(int deltaX) {//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;
	}
}

中间还用到的一个事件监听接口

public interface OnViewChangeListener {
	public void OnViewChange(int view);
}

布局文件:




    

        

            
        

        

            
        

        

        

            

这里需要注意的是,下面的ImageView的个数必须与上面的导航页面数相同。因为我当时在使用时还未搞明白就直接按需求改,出现数据溢出的情况。


自定义的容器实现了viewpaper的功能,不知道当时做的时候为啥没有使用viewpaper来实现。

点击最后一个导航页上面的按钮进入登陆界面,并且标记应用已经被使用过了。下次启动时就不会出现导航页面了。

你可能感兴趣的:(Android项目)