Android新闻类导航栏

一个新闻导航栏,可以自定义文字颜色、选中文字和未选中文字大小、文字间距、拖拽效果等效果。


一、先上效果图

Android新闻类导航栏_第1张图片
A

Android新闻类导航栏_第2张图片
B

二、GitHub

代码地址,欢迎指正https://github.com/MNXP/FlexTitle

三、A的使用

1)A的引用

   

2)A的自定义

   
            
        //选中文字大小
        //未选择文字颜色
        //选择文字颜色
        //背景颜色
        //线 起始颜色
        //线 终止颜色
        //线的间距
        //item间距
        //据上高度
        //距下高度
        //线 距下高度
        // 线的高度
        //文字是否左右均留空隙
        //线是否有拖拽效果
    

3)A的实现

根据titleCenter和lineDrag来判断线的起始点和终点绘制
if (titleCenter) {
            if (lineDrag) {
                if (lastPosition > position) {
                    for (int i = 0; i < position; i++) {
                        leftAll = leftAll + getDefaultWidth(i);
                    }
                    for (int i = 0; i < lastPosition; i++) {
                        rightAll = rightAll + getDefaultWidth(i);
                    }
                    left = leftAll + (position * 2 + 1) * dis + positionOffset * (getDefaultWidth(position) + 2 * dis) + lineMargins;
                    right = rightAll + (lastPosition * 2 + 1) * dis + getDefaultWidth(lastPosition) + lineMargins;
                    dynamicLine.updateView(left, right);
                } else {
                    if (positionOffset > 0.5f) {
                        positionOffset = 0.5f;
                    }
                    for (int i = 0; i < position; i++) {
                        leftAll = leftAll + getDefaultWidth(i);
                    }
                    for (int i = 0; i <= position; i++) {
                        rightAll = rightAll + getDefaultWidth(i);
                    }
                    left = leftAll + (position * 2 + 1) * dis + lineMargins;
                    right = rightAll + (position * 2 + 1) * dis + lineMargins + positionOffset * 2 * (getDefaultWidth(position + 1) + 2 * dis);
                    dynamicLine.updateView(left, right);
                }
            } else {

                if (lastPosition > position) {
                    for (int i = 0; i < position; i++) {
                        leftAll = leftAll + getDefaultWidth(i);
                    }
                    for (int i = 0; i < lastPosition; i++) {
                        rightAll = rightAll + getDefaultWidth(i);
                    }
                    left = leftAll + (position + positionOffset) * 2 * dis + dis + lineMargins + positionOffset * (lastWidth - lastDis);
                    right = rightAll + (position + positionOffset) * 2 * dis + dis + lineMargins + positionOffset * lastWidth;
                    dynamicLine.updateView(left, right);
                } else {
                    for (int i = 0; i < position; i++) {
                        leftAll = leftAll + getDefaultWidth(i);
                    }
                    for (int i = 0; i <= position; i++) {
                        rightAll = rightAll + getDefaultWidth(i);
                    }
                    left = dis + leftAll + position * 2 * dis + lineMargins + positionOffset * 2 * dis + positionOffset * (lastWidth - lastDis);
                    right = dis + rightAll + position * 2 * dis + lineMargins + positionOffset * 2 * dis + positionOffset * lastWidth;
                    dynamicLine.updateView(left, right);
                }
            }

        } else {
            if (lineDrag) {

                if (lastPosition > position) {
                    for (int i = 0; i < position; i++) {
                        leftAll = leftAll + getDefaultWidth(i);
                    }
                    for (int i = 0; i < lastPosition; i++) {
                        rightAll = rightAll + getDefaultWidth(i);
                    }
                    left = leftAll + (position + 1) * dis + positionOffset * (getDefaultWidth(position) + dis) + lineMargins;
                    right = rightAll + (lastPosition + 1) * dis + getDefaultWidth(lastPosition) + lineMargins;
                    dynamicLine.updateView(left, right);
                } else {
                    if (positionOffset > 0.5f) {
                        positionOffset = 0.5f;
                    }
                    for (int i = 0; i < position; i++) {
                        leftAll = leftAll + getDefaultWidth(i);
                    }
                    for (int i = 0; i <= position; i++) {
                        rightAll = rightAll + getDefaultWidth(i);
                    }
                    left = leftAll + (position + 1) * dis + lineMargins;
                    right = rightAll + (position + 1) * dis + lineMargins + positionOffset * 2 * (getDefaultWidth(position + 1) + dis);
                    dynamicLine.updateView(left, right);
                }

            } else {
                if (lastPosition > position) {
                    for (int i = 0; i < position; i++) {
                        leftAll = leftAll + getDefaultWidth(i);
                    }
                    for (int i = 0; i < lastPosition; i++) {
                        rightAll = rightAll + getDefaultWidth(i);
                    }
                    left = leftAll + (position + positionOffset) * dis + dis + lineMargins + positionOffset * (lastWidth - lastDis);
                    right = rightAll + (position + positionOffset) * dis + dis + lineMargins + positionOffset * lastWidth;
                    dynamicLine.updateView(left, right);
                } else {
                    for (int i = 0; i < position; i++) {
                        leftAll = leftAll + getDefaultWidth(i);
                    }
                    for (int i = 0; i <= position; i++) {
                        rightAll = rightAll + getDefaultWidth(i);
                    }
                    left = dis + leftAll + position * dis + lineMargins + positionOffset * dis + positionOffset * (lastWidth - lastDis);
                    right = dis + rightAll + position * dis + lineMargins + positionOffset * dis + positionOffset * lastWidth;
                    dynamicLine.updateView(left, right);
                }
            }

        }
通过判断,进行不同的逻辑处理(代码位置com.xp.different.MyOnPageChangeListener) 

四、B的使用

这种是借鉴的,如有侵犯,请联系,会及时删除

1)B的引用

   

2)B的自定义

  
        
        
        
        
        
        
        
        
        //线 起始颜色
        //线 终止颜色
        // 线的高度
        //线 距下高度
    

3)B的实现

根据位置绘制文字和线
        //画正常的文字内容
        paint.setTextSize(textSize);
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        canvas.save();
        paint.setColor(defaultColor);
        canvas.drawText(text, textLeft, textBottom - fontMetrics.descent, paint);
        canvas.restore();
        //画渐变部分的文字
        canvas.save();
        paint.setColor(changeColor);
        canvas.clipRect(startX, 0, endX, getMeasuredHeight());
        canvas.drawText(text, textLeft, textBottom - fontMetrics.descent, paint);
        canvas.restore();
(代码位置com.xp.shadow.ShadowTextView)

参考仿微博导航条
初次尝试,海涵。如有意见和建议,及时沟通。

你可能感兴趣的:(Android新闻类导航栏)