花里胡哨系列之-----------------------------点击带动画效果的底部导航,图片文字

最近看GitHub上面有一个开源的库,是一个带动画效果的底部导航,不点击的时候是一个图片,点击就会出现图片加文字的效果。

下面是GitHub地址:

https://github.com/gauravk95/bubble-navigation

 

本人自己写了一个小Demo:源码附上

1.导入依赖:

implementation 'com.gauravk.bubblenavigation:bubblenavigation:1.0.7'

2.首先是Activity部分:

public class MainActivity extends AppCompatActivity {
    public List<Fragment> fragmentList = new ArrayList<>();
    private FragmentA fragmentA;
    private FragmentB fragmentB;
    private FragmentC fragmentC;
    private FragmentD fragmentD;
    private BubbleToggleView itemA;
    private BubbleToggleView itemB;
    private BubbleToggleView itemC;
    private BubbleToggleView itemD;
    private List<BubbleToggleView>viewList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BubbleNavigationLinearView bubbleNavigationLinearView = (BubbleNavigationLinearView) findViewById(R.id.constrainView);
        //初始化BubbleToggleView
        initView();
        //初始化Fragment
        initFragment();

        //指定跳转Framgnet的方法
        showFragment(fragmentList.get(0));
        isActivity(0);
        bubbleNavigationLinearView.setNavigationChangeListener(new BubbleNavigationChangeListener() {
            @Override
            public void onNavigationChanged(View view, int position) {
                switch (position) {
                    case 0:
                        showFragment(fragmentList.get(0));
                        isActivity(position);
                        break;
                    case 1:
                        showFragment(fragmentList.get(1));
                        isActivity(position);
                        break;
                    case 2:
                        showFragment(fragmentList.get(2));
                        isActivity(position);
                        break;
                    case 3:
                        showFragment(fragmentList.get(3));
                        isActivity(position);
                        break;
                }
            }
        });

    }

    //是否选中
    private void isActivity(int position) {
        for(int i = 0 ; i < viewList.size() ; i++){
            if(i == position){
                viewList.get(i).setActivated(true);
            }else{
                viewList.get(i).setActivated(false);
            }
        }
    }

    private void initView() {
        itemA = (BubbleToggleView) findViewById(R.id.item1);
        itemB = (BubbleToggleView) findViewById(R.id.item2);
        itemC = (BubbleToggleView) findViewById(R.id.item3);
        itemD = (BubbleToggleView) findViewById(R.id.item4);
        viewList.add(itemA);
        viewList.add(itemB);
        viewList.add(itemC);
        viewList.add(itemD);
    }


    private void showFragment(Fragment fragment) {
        jumpFragment(fragment);
    }

    private void jumpFragment(Fragment fragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction action = manager.beginTransaction();
        // 如果不为空,就先隐藏起来
        hideFragment(action);
        if (!fragment.isAdded()) {
            action.add(R.id.re_layout, fragment);
        }
        action.show(fragment);
        action.commit();
    }

    private void hideFragment(FragmentTransaction ft) {
        if (fragmentA != null) {
            ft.hide(fragmentA);
        }
        if (fragmentB != null) {
            ft.hide(fragmentB);
        }

        if (fragmentC != null) {
            ft.hide(fragmentC);
        }

        if (fragmentD != null) {
            ft.hide(fragmentD);
        }

    }

    private void initFragment() {
        if (fragmentA == null) {
            fragmentA = new FragmentA();
        }
        if (fragmentB == null) {
            fragmentB = new FragmentB();
        }
        if (fragmentC == null) {
            fragmentC = new FragmentC();
        }
        if (fragmentD == null) {
            fragmentD = new FragmentD();
        }
        fragmentList.add(fragmentA);
        fragmentList.add(fragmentB);
        fragmentList.add(fragmentC);
        fragmentList.add(fragmentD);
    }
}

3.Activity的xml部分:这个分为两种布局一种是BubbleNavigationConstraintView继承ConstraintView的,我这里是使用BubbleNavigationLinearView看哪个顺手用哪个。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mdj.acer.motejiatest.MainActivity">

    <com.gauravk.bubblenavigation.BubbleNavigationLinearView
        android:id="@+id/constrainView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#fff"
        android:elevation="6dp"
        android:orientation="horizontal"
        android:padding="10dp">

        <com.gauravk.bubblenavigation.BubbleToggleView
            android:id="@+id/item1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:bt_active="false"
            app:bt_colorActive="#0ad6ff"
            app:bt_colorInactive="#c0c0c0"
            app:bt_icon="@drawable/weixuanzhong_home"
            app:bt_padding="3dp"
            app:bt_title="首页"
            app:bt_titlePadding="3dp"
            app:bt_titleSize="36px"></com.gauravk.bubblenavigation.BubbleToggleView>

        <com.gauravk.bubblenavigation.BubbleToggleView
            android:id="@+id/item2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:bt_active="false"
            app:bt_colorActive="#0ad6ff"
            app:bt_colorInactive="#c0c0c0"
            app:bt_icon="@drawable/weixuanzhong_gengduo"
            app:bt_padding="3dp"
            app:bt_title="首页"
            app:bt_titlePadding="3dp"
            app:bt_titleSize="36px"></com.gauravk.bubblenavigation.BubbleToggleView>

        <com.gauravk.bubblenavigation.BubbleToggleView
            android:id="@+id/item3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:bt_active="false"
            app:bt_colorActive="#0ad6ff"
            app:bt_colorInactive="#c0c0c0"
            app:bt_icon="@drawable/weixuanzhong_faxian"
            app:bt_padding="3dp"
            app:bt_title="首页"
            app:bt_titlePadding="3dp"
            app:bt_titleSize="36px"></com.gauravk.bubblenavigation.BubbleToggleView>

        <com.gauravk.bubblenavigation.BubbleToggleView
            android:id="@+id/item4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:bt_active="false"
            app:bt_colorActive="#0ad6ff"
            app:bt_colorInactive="#c0c0c0"
            app:bt_icon="@drawable/weixianzhong_wode"
            app:bt_padding="3dp"
            app:bt_title="首页"
            app:bt_titlePadding="3dp"
            app:bt_titleSize="36px"></com.gauravk.bubblenavigation.BubbleToggleView>


    </com.gauravk.bubblenavigation.BubbleNavigationLinearView>

    <RelativeLayout
        android:id="@+id/re_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/constrainView"></RelativeLayout>

</RelativeLayout>

4.Fragment的部分,由于Fragment写的都一样,就贴出一个:

public class FragmentA extends Fragment{
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_a, container,false);
        return inflate;
    }
}

5.Fragment的xml部分:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#c0c0c0"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="I am Fragment A"
        android:textSize="36dp"
        android:textColor="#000"
        />

</RelativeLayout>

6.贴出BubbleToggleView一些属性:

bt_active设置为活动状态
Bt _ ColorActivity处于活动状态时,将此颜色用于图标和标题
bt_colorInctive处于非活动状态时,将此颜色用于图标和标题
bt _icon设置图标可绘制
bt_iconWidth更新图标宽度
bt _iconHight更新图标高度
bt_title设置标题文本
bt_titleSize更新sp中的倾斜文本大小
bt_shape设置可绘制的背景。使用transitiondrawinable切换时获得淡入淡出效果
bt _ showShapeAlways如果为真,并使用正常绘制,背景形状始终可见
bt_shapeColor更改形状的色调。当使用TransitionDrawable或showShapeAlways时,不适用始终为真。
bt _duration设置切换动画以毫秒为单位完成的持续时间
bt_padding设置dp中的内部填充
bt _titlePadding在dp中设置标题填充
bt_badgeText设置徽章的文本
bt_badgeTextSize设置徽章文本的字体大小
bt_badgeTextColor设置徽章的文本颜色
bt_badgeBackgroundColor设置徽章的背景颜色
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值