android 菜单栏 仿微信 自定义View初阶学习

项目里的自定View 参考前人 只是为了方便自已以后再次使用 所以记录下
感谢前人种树!感谢前人种树!感谢前人种树!
第一次写博客…废话不多说开始.

在原载上添加对自已比较实用的功能

支持菜单栏里显示三种模式: 图标和标题显示; 图标单独显示; 标题单独显示

必须要设置的
/**
* 设置Fragment容器
* @param containerId 容器Id
* @return
*/
public MenuBar setContainer(int containerId) {
this.containerId = containerId;
return this;
}

必须要设置的
/**
* @param mode 0 黙认模式 显示图标与标题
* 1 只显示图标模式
* 2 只显示文字模式
* @return
*/
public MenuBar setMode(int mode) {
this.mode = mode;
return this;
}

/** 重载这个函数
* @param fClass Fragment class 对象
* @param title 菜单栏对象名称
* @param iconResBefore 菜单栏默认图标
* @param iconResAfter 菜单栏点击图标
* @return BottomBar对象
*/
public MenuBar addItem(Class fClass, String title, int iconResBefore, int iconResAfter)

/**
 * @param beforeColor  黙认着色
 * @param afterColor  点击后的着色
 * @return
 */

public MenuBar setTitleBeforeAndAfterColor(String beforeColor, String afterColor)

**build 完成了 **
public void build() {…}

下面 看看基本如何使用
MenuBar bt = (MenuBar) findViewById(R.id.bt);
bt.setContainer(R.id.container) .addItem(Fragment_Car_Case.class,“车 况”,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher)
.addItem(Fragment_Car_Checkout.class,“检查”,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher)
.addItem(Fragment_Car_Map.class,“地图”,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher)
.addItem(Fragment_Car_User.class,“用户”,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher)
.setMode(0)
.build();

对 就这么简单就可以完成菜单栏 addItem() 这里不是一定要四个 想要写几个都行

下图:

在这里插入图片描述
在这里插入图片描述

附上主要原代码

//自定义属性 想用就用 不用也可以 有默认
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (widthMode == MeasureSpec.AT_MOST) {
        //TODO wrap_content 黙认值
        width = 300;
    } else {
        //TODO 指定大小或者match_parent
        width = widthSize;
    }

    if (heightMode == MeasureSpec.AT_MOST) {
        //TODO wrap_content 黙认值
        height = 200;
    } else {
        //TODO 指定大小或者match_parent
        height = heightSize;
    }
    setMeasuredDimension(width, height);

}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);

    if (itemCout != 0) {
        if (mode == 0) {
            defaultMode();  //显示图标和标题模式
        } else if (mode == 1) {
            modePic(); //显示图标模式
        } else if (mode == 2) {
            modeText();//显示标题模式
        }
    }
}

private void modeText() {
int itemWidth = width / itemCout;
int itemHeight = height;
int textSize = dp2px(titleSize);
paint.setTextSize(textSize);
Rect rect = new Rect();
for (int i = 0; i < itemCout; i++) {
String title = titleList.get(i);
paint.getTextBounds(title, 0, title.length(), rect);
titleXList.add((itemWidth - rect.width()) / 2 + itemWidth * i);
titleBaseLine = itemHeight - (itemHeight - rect.height()) / 2;
}
}

private void modePic() {
    int itemWidth = width / itemCout;
    int itemHeight = height;
    int iconWidth = dp2px(this.iconWidth);
    int iconheight = dp2px(this.iconheight);

    int iconTop = (itemHeight - iconheight) / 2;
    int firstRectX = (itemWidth - iconWidth) / 2;
    for (int i = 0; i < itemCout; i++) {
        int rectX = i * itemWidth + firstRectX;
        Rect temp = iconRectList.get(i);
        temp.left = rectX;
        temp.top = iconTop;
        temp.right = rectX + iconWidth;
        temp.bottom = iconTop + iconheight;
    }
}

private void defaultMode() {
    int itemWidth = width / itemCout;
    int itemHeight = height;

    int iconWidth = dp2px(this.iconWidth);
    int iconheight = dp2px(this.iconheight);

    int textIconMargin = dp2px(titleIconMargin);

    int textSize = dp2px(titleSize);

    paint.setTextSize(textSize);

    Rect rect = new Rect();
    paint.getTextBounds(titleList.get(0), 0, titleList.get(0).length(), rect);  //返回文本所需要宽高

    int titleTextWidth = rect.width();
    int titleTextHeight = rect.height();

    int iconTop = (itemHeight - iconheight - textIconMargin - titleTextHeight) / 2;//icon marginTop 高度
    titleBaseLine = itemHeight - iconTop;

    int firstRectX = (itemWidth - iconWidth) / 2;
    for (int i = 0; i < itemCout; i++) {
        int rectX = i * itemWidth + firstRectX;
        Rect temp = iconRectList.get(i);
        temp.left = rectX;
        temp.top = iconTop;
        temp.right = rectX + iconWidth;
        temp.bottom = iconTop + iconheight;
    }

    for (int i = 0; i < itemCout; i++) {
        String title = titleList.get(i);
        paint.getTextBounds(title, 0, title.length(), rect);
        titleXList.add((itemWidth - rect.width()) / 2 + itemWidth * i);

    }
}

//重写事件要处理点击事件

@Override
public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            int x = (int) event.getX();
            //按下时的区域
            index = x / (width / itemCout);
            break;
        case MotionEvent.ACTION_UP:
            x = (int) event.getX();
            //抬起时的区域  在同一个区域才视为点击事件
            if (index == (x / (width / itemCout))) {
                switchFragment(index);
                invalidate();
            }
            index = -1;
            break;
    }

    return true;
} 

//绘画
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

    if (itemCout != 0) {

        if (mode == 0 || mode == 1) {

            paint.setAntiAlias(false);
            for (int i = 0; i < itemCout; i++) {

                Bitmap bitmap = null;
                if (i == currentCheckedIndex) {
                    bitmap = iconAterBitmap.get(i);
                } else {
                    bitmap = iconBeforeBitmap.get(i);
                }
                canvas.drawBitmap(bitmap, null, iconRectList.get(i), paint);
            }
        }
        if (mode == 0 || mode == 2) {
            paint.setAntiAlias(true);
            for (int i = 0; i < itemCout; i++) {
                String title = titleList.get(i);
                if (i == currentCheckedIndex) {
                    paint.setColor(titleColorAfter);
                } else {
                    paint.setColor(titleColorBefore);
                }
                canvas.drawText(title, titleXList.get(i), titleBaseLine, paint);
            }
        }

    }
}

//选择切换Fragment
private Fragment currentFragment = null;
private void switchFragment(int index) {

    this.currentCheckedIndex = index;

    Fragment fragment = fragmentList.get(index);
    if (fragment != null) {
        FragmentTransaction transaction = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
        if (fragment.isAdded()){
            if (currentFragment != null){
                transaction.hide(currentFragment).show(fragment);
            }else {
                transaction.show(fragment);
            }
        }else {
            if (currentFragment != null){
                transaction.hide(currentFragment).add(containerId,fragment);

            }else {
                transaction.add(containerId,fragment);
            }
        }
        currentFragment = fragment;
        transaction.commit();
    }

    invalidate();
}

第一次写 乱得很 只是为了记录 不会排版… =_=!

项目原码下载:https://download.csdn.net/download/scorpio_alex_/11025353

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值