您当前的位置: 首页 > 

悬浮指示器布局-自定义控件

发布时间:2016-10-19 18:28:12 ,浏览量:0

这是一个悬浮 【指示器 - 菜单】的控件容器,支持 上滑|下滑 的百分比进度跟踪,去掉了对 layout布局文件的依赖,

去掉了对values 属性文件的依赖,由于第二次维护这个小demo了,以后会的多了,在改进。。。

FloatTitleLayout.gif

核心代码如下

/*处理滑动事件:开始*/
if (Math.abs(distanceX) > Math.abs(distanceY * 0.8)) {
    /*上下滑动的  距离 表现不明显*/
    return super.dispatchTouchEvent(event);
}
//KLog.e("isFloated = "+isFloated+" isChildOnTop = "+isChildOnTop);
 /*非悬浮状态,向上滑动↓*/
if ((!isFloated) && (distanceY > 0)) {
   /*非悬浮状态,向上滑动,速度过大↓*/
    if (yVelocity < -yVelocityMax) {
        int indicatorScrollDistanceCurr = 0;
        /*非悬浮状态,向上滑动,速度过大,导航条在初始状态,直接将导航条,全部拖上去↓*/
        if (paramsIvTopBackground.topMargin == 0) {
            distanceY = titleHeight - ivTopBackground.getHeight();
            indicatorScrollDistanceCurr = indicatorScrollDistanceMax;
        } else {
            /*非悬浮状态,向上滑动,速度过大,导航条不在初始状态,直接将导航条,全部拖上去↑*/
            distanceY = -locationIndicatorYInScreen + titleHeight + statusHeight;
            indicatorScrollDistanceCurr = indicatorScrollDistanceMax;
        }
        paramsIvTopBackground.topMargin = paramsIvTopBackground.topMargin + (int) distanceY;
        ivTopBackground.setLayoutParams(paramsIvTopBackground);
        updateScrollProgress(indicatorScrollDistanceCurr);
        isFloated = true;
        return true;
    } else if (yVelocity >= -yVelocityMax) {
        /*非悬浮状态,向上滑动,速度正常↓*/
        if (titleHeight + statusHeight + distanceY > locationIndicatorYInScreen) {
            distanceY = locationIndicatorYInScreen - titleHeight - statusHeight;
        }
        paramsIvTopBackground.topMargin = paramsIvTopBackground.topMargin - (int) distanceY;
        ivTopBackground.setLayoutParams(paramsIvTopBackground);
        /*计算后得到当前真实的  locationIndicatorYInScreen*/
        int locationIndicatorYInScreenTrue = (locationIndicatorYInScreen - (int) distanceY);
        int indicatorScrollDistanceCurr = ivTopBackground.getHeight() + statusHeight - locationIndicatorYInScreenTrue;
        //KLog.e("计算后的 locationIndicatorYInScreen "+locationIndicatorYInScreenTrue+" indicatorScrollDistanceCurr = "+indicatorScrollDistanceCurr);
        //KLog.e("背景图高度 = "+ivTopBackground.getHeight()+" locationIvTopBackgroundYInScreen = "+locationIvTopBackgroundYInScreen+" locationIndicatorYInScreen = " + locationIndicatorYInScreen + " statusHeight = " + statusHeight + " titleHeight = " + titleHeight+" distanceY = "+distanceY+" indicatorScrollDistanceMax = "+indicatorScrollDistanceMax);
        updateScrollProgress(indicatorScrollDistanceCurr);
        return true;
    }
} else if ((!isFloated) && (distanceY < 0)) {
      /*非悬浮状态,向下滑动 ↑*/
    //&& isChildOnTop
    int indicatorScrollDistanceCurr = 0;
    if (locationIndicatorYInScreen == (ivTopBackground.getHeight() + statusHeight)) {
        /*非悬浮状态,向下滑动 ,当前导航条在初始状态↑*/
        return super.dispatchTouchEvent(event);
    }
    if (yVelocity > yVelocityMax) {
        /*非悬浮状态,向下滑动,速度过大*/
        indicatorScrollDistanceCurr = 0;
        paramsIvTopBackground.topMargin = 0;
    } else if (yVelocity < yVelocityMax) {
        /*非悬浮状态,向下滑动 ,速度正常*/
        //KLog.e("topMargin = "+paramsIvTopBackground.topMargin+" locationIndicatorYInScreen = "+locationIndicatorYInScreen+" distanceY = "+distanceY);
        if ((locationIndicatorYInScreen - (int) distanceY) <= (ivTopBackground.getHeight() + statusHeight)) {
            paramsIvTopBackground.topMargin = paramsIvTopBackground.topMargin - (int) distanceY;
            indicatorScrollDistanceCurr = ivTopBackground.getHeight() + statusHeight - (locationIndicatorYInScreen - (int) distanceY);
        } else {
            paramsIvTopBackground.topMargin = 0;
            indicatorScrollDistanceCurr = 0;
        }
    }
    ivTopBackground.setLayoutParams(paramsIvTopBackground);
    updateScrollProgress(indicatorScrollDistanceCurr);
    return true;
} else if (isFloated && (distanceY < 0) && isChildOnTop) {
    isFloated = false;
    int indicatorScrollDistanceCurr = 0;
     /*悬浮状态,向下滑动,ScrollView 展示第一条数据 ↑*/
    if (yVelocity > yVelocityMax) {
        /*悬浮状态,向下滑动,ScrollView 展示第一条数据 ,速度过大,直接将背景图,全部拖出来*/
        indicatorScrollDistanceCurr = 0;
        paramsIvTopBackground.topMargin = 0;
    } else if (yVelocity <= yVelocityMax) {
        /*悬浮状态,向下滑动,ScrollView 展示第一条数据 ,速度正常,将背景图,慢慢拖出来*/
        //KLog.e("distanceY = "+distanceY+" locationIndicatorYInScreen = "+locationIndicatorYInScreen);
        if ((locationIndicatorYInScreen - (int) distanceY) <= (ivTopBackground.getHeight() + statusHeight)) {
            paramsIvTopBackground.topMargin = paramsIvTopBackground.topMargin - (int) distanceY;
            indicatorScrollDistanceCurr = ivTopBackground.getHeight() + statusHeight - (locationIndicatorYInScreen - (int) distanceY);
        } else {
            paramsIvTopBackground.topMargin = ivTopBackground.getHeight() + statusHeight;
            indicatorScrollDistanceCurr = indicatorScrollDistanceMax;
        }
    }
    ivTopBackground.setLayoutParams(paramsIvTopBackground);
    updateScrollProgress(indicatorScrollDistanceCurr);
}
/*处理滑动事件:结束*/

此时此刻,很开心功能可以实现了,不用被鄙视了。

国际惯例 附上源码

https://github.com/Alex-Cin/FloatIndicatorLayout

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    111326博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0496s