转:http://blog.csdn.net/xiaanming/article/details/17374599html
很荣幸我可以成为CSDN 2013年度博客之星评选的候选人,但愿继续获得你们的支持与鼓励,看到的朋友帮我投上宝贵的一票吧!java
投票地址:http://vote.blog.csdn.net/blogstaritem/blogstar2013/xiaanmingandroid
转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming),请尊重他人的辛勤劳动成果,谢谢!app
随着移动互联网的快速发展,它已经和咱们的生活息息相关了,在公交地铁里面都能看到不少人的人低头看着本身的手机屏幕,今后“低头族”一词就产生了,做为一名移动行业的开发人员,我本身也是一名“低头族”,上下班时间在公交地铁上看看新闻来打发下时间,有时候也会看看那些受欢迎的App的一些界面效果,为何人家的app那么受欢迎?跟用户体验跟UI设计也有直接的关系,最近在美团和大众点评的App看到以下效果,我感受用户好,很人性化,因此本身也尝试着实现了下,接下来就讲解下实现思路!ide


如上图(2)咱们看到了,当当即抢购布局向上滑动到导航栏布局的时候,当即抢购布局就贴在导航栏布局下面,下面的其余的布局仍是能够滑动,当咱们向下滑动的时候,当即抢购的布局又随着往下滑动了,看似有点复杂,可是一说思路可能你就顿时恍然大悟了。布局
当咱们向上滑动过程当中,咱们判断当即抢购的布局是否滑到导航栏布局下面,若是当即抢购的上面顶到了导航栏,咱们新建一个当即抢购的悬浮框来显示在导航栏下面,这样子就实现了当即抢购贴在导航栏下面的效果啦,而当咱们向下滑动的时候,当当即抢购布局的下面恰好到了刚刚新建的当即抢购悬浮框的下面的时候,咱们就移除当即抢购悬浮框,可能说的有点拗口,既然知道了思路,接下来咱们就来实现效果。this
新建一个Android项目,取名MeiTuanDemo,先看当即抢购(buy_layout.xml)的布局,这里为了方便我直接从美团上面截去了图片spa
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
-
- <ImageView
- android:id="@+id/buy_layout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/buy" />
-
- </LinearLayout>
当即抢购的布局实现了,接下来实现主界面的布局,上面是导航栏布局,为了方便仍是直接从美团截取的图片,而后下面的ViewPager布局,当即抢购布局,其余布局 放在ScrollView里面,界面仍是很简单的.net
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
-
- <ImageView
- android:id="@+id/imageView1"
- android:scaleType="centerCrop"
- android:layout_width="match_parent"
- android:layout_height="45dip"
- android:src="@drawable/navigation_bar" />
-
-
- <com.example.meituandemo.MyScrollView
- android:id="@+id/scrollView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
-
- <ImageView
- android:id="@+id/iamge"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/pic"
- android:scaleType="centerCrop" />
-
- <include
- android:id="@+id/buy"
- layout="@layout/buy_layout" />
-
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/one"
- android:scaleType="centerCrop" />
-
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/one"
- android:scaleType="centerCrop" />
-
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/one"
- android:scaleType="centerCrop" />
- </LinearLayout>
- </com.example.meituandemo.MyScrollView>
-
- </LinearLayout>
你会发现上面的主界面布局中并非ScrollView,而是自定义的一个MyScrollView,接下来就看看MyScrollView类中的代码设计
- package com.example.meituandemo;
-
- import android.content.Context;
- import android.os.Handler;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.widget.ScrollView;
-
-
-
-
-
-
- public class MyScrollView extends ScrollView {
- private OnScrollListener onScrollListener;
-
-
-
- private int lastScrollY;
-
- public MyScrollView(Context context) {
- this(context, null);
- }
-
- public MyScrollView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- }
-
- public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
-
-
-
-
-
- public void setOnScrollListener(OnScrollListener onScrollListener) {
- this.onScrollListener = onScrollListener;
- }
-
-
-
-
-
- private Handler handler = new Handler() {
-
- public void handleMessage(android.os.Message msg) {
- int scrollY = MyScrollView.this.getScrollY();
-
-
- if(lastScrollY != scrollY){
- lastScrollY = scrollY;
- handler.sendMessageDelayed(handler.obtainMessage(), 5);
- }
- if(onScrollListener != null){
- onScrollListener.onScroll(scrollY);
- }
-
- };
-
- };
-
-
-
-
-
-
-
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- if(onScrollListener != null){
- onScrollListener.onScroll(lastScrollY = this.getScrollY());
- }
- switch(ev.getAction()){
- case MotionEvent.ACTION_UP:
- handler.sendMessageDelayed(handler.obtainMessage(), 5);
- break;
- }
- return super.onTouchEvent(ev);
- }
-
-
-
-
-
-
-
-
-
- public interface OnScrollListener{
-
-
-
-
-
- public void onScroll(int scrollY);
- }
-
-
-
- }
一看代码你也许明白了,就是对ScrollView的滚动Y值进行监听,咱们知道ScrollView并无实现滚动监听,因此咱们必须自行实现对ScrollView的监听,咱们很天然的想到在onTouchEvent()方法中实现对滚动Y轴进行监听,但是你会发现,咱们在滑动ScrollView的时候,当咱们手指离开ScrollView。它可能还会继续滑动一段距离,因此咱们选择在用户手指离开的时候每隔5毫秒来判断ScrollView是否中止滑动,并将ScrollView的滚动Y值回调给OnScrollListener接口的onScroll(int scrollY)方法中,咱们只须要对ScrollView调用咱们只须要对ScrollView调用setOnScrollListener方法就能监听到滚动的Y值。
实现了对ScrollView滚动的Y值进行监听,接下来就简单了,咱们只须要显示当即抢购悬浮框和移除悬浮框了,接下来看看主界面Activity的代码编写
上面的代码比较简单,根据ScrollView滑动的距离来判断显示和移除悬浮框,悬浮框的实现主要是经过WindowManager这个类来实现的,调用这个类的addView方法用于添加一个悬浮框,removeView用于移除悬浮框。
经过上述代码就实现了美团,大众点评的这种效果,在运行项目以前咱们必须在AndroidManifest.xml中加入<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
咱们运行下项目看下效果吧

好了,今天的讲解到此结束,有疑问的朋友请在下面留言
项目源码,点击下载
PS:你们有兴趣的话能够看看Android 仿美团网,大众点评购买框悬浮效果之修改版