仿抖音底部导航效果(一)

简介: 最终效果预览最终效果这次实现的是第一步效果本次效果原理解析:通过对控件添加动画来实现仿抖音底部导航的效果一.首先编写布局文件(这里是用TextView作为底部的指示横线) 二.

最终效果预览

img_c051bc0dbfcdceb2d8f5f4b7939d9b2c.gif
最终效果

这次实现的是第一步效果

img_59a5549a3361667b04a3af0d049c1f06.gif
本次效果

<h3 style="color:blue">原理解析:通过对控件添加动画来实现仿抖音底部导航的效果</h3>

一.首先编写布局文件(这里是用TextView作为底部的指示横线)

<?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">
    <LinearLayout
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:id="@+id/nav_top"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:text="标题"
            android:id="@+id/nav_item_tv_title"
            android:textColor="#F1F1F1"
            android:textSize="16sp"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:id="@+id/nav_item_tv_line"
        android:layout_below="@id/nav_top"
        android:layout_marginTop="2dp"
        android:layout_alignRight="@id/nav_top"
        android:layout_alignLeft="@id/nav_top"
        android:background="#fff"
        android:alpha="0"
        android:layout_centerInParent="true"
        android:layout_height="2dp" />
</RelativeLayout>

二.创建NavItemView类继承RelativeLayout(这是最终代码展示,接下来讲解各种方法的效果【除了自定义属性】)

public class NavItemView extends RelativeLayout {
    TextView textView_title;
    TextView textView_line;
    private View mView;
    public NavItemView(Context context) {
        this(context,null);
    }
    public NavItemView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    @SuppressLint("ResourceType")
    public NavItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mView = View.inflate(context, R.layout.view_navitem, this);
        textView_line=mView.findViewById(R.id.nav_item_tv_line);
        textView_title=mView.findViewById(R.id.nav_item_tv_title);
//        获取自定义属性
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.NavItem);
        if(ta!=null){
            String navtext = ta.getString(R.styleable.NavItem_navtext);
            textView_title.setText(navtext);
        }
    }
//    选中状态
    public void startActive(){
        textView_title.setTextColor(Color.WHITE);
        textView_title.setTextSize(18);
        textView_line.animate().alpha(1).setDuration(200).start();
    }
//    取消选中
    public void cancelActive(){
        textView_title.setTextColor(Color.parseColor("#F1F1F1"));
        textView_title.setTextSize(16);
        textView_line.animate().alpha(0).setDuration(200).start();
    }
}

三.startActive()方法作用:设置TextView的颜色为白色,并通过动画的方式显示底部指示线,达到选中状态。

四.cancelActive()方法作用:与startActive()方法的作用相反。

五.Activity调用

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    NavItemView navItemView1,navItemView2,navItemView3,navItemView4,navItemView_Temp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navItemView1=findViewById(R.id.one);
        navItemView2=findViewById(R.id.two);
        navItemView3=findViewById(R.id.three);
        navItemView4=findViewById(R.id.four);
        navItemView1.setOnClickListener(this);
        navItemView2.setOnClickListener(this);
        navItemView3.setOnClickListener(this);
        navItemView4.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if(navItemView_Temp!=null){
            navItemView_Temp.cancelActive();
        }
        switch (v.getId()){
            case R.id.one:
                navItemView1.startActive();
                navItemView_Temp=navItemView1;
                break;
            case R.id.two:
                navItemView2.startActive();
                navItemView_Temp=navItemView2;
                break;
            case R.id.three:
                navItemView3.startActive();
                navItemView_Temp=navItemView3;
                break;
            case R.id.four:
                navItemView4.startActive();
                navItemView_Temp=navItemView4;
                break;
        }
    }
}

六.自定义属性文件展示

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="NavItem">
        <attr name="navtext" format="string"/>
    </declare-styleable>
</resources>

七.Activity布局文件展示

<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#000"
tools:context="com.example.douyin.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_height="40dp">
        <com.example.douyin.NavItemView
            android:layout_width="0dp"
            android:id="@+id/one"
            app:navtext="首页"
            android:layout_weight="1"
            android:layout_height="wrap_content">
        </com.example.douyin.NavItemView>
        <com.example.douyin.NavItemView
            android:layout_width="0dp"
            android:id="@+id/two"
            app:navtext="关注"
            android:layout_weight="1"
            android:layout_height="wrap_content">
        </com.example.douyin.NavItemView>
        <com.example.douyin.NavItemView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/three"
            app:navtext="消息"
            android:layout_height="wrap_content">
        </com.example.douyin.NavItemView>
        <com.example.douyin.NavItemView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/four"
            app:navtext="我"
            android:layout_height="wrap_content">
        </com.example.douyin.NavItemView>
    </LinearLayout>

</RelativeLayout>
目录
相关文章
|
存储 容器
仿百度福袋红包界面
仿百度福袋红包界面
71 0
仿百度福袋红包界面
|
JavaScript 前端开发
仿抖音短视频系统源码,点击实现波纹效果
仿抖音短视频系统源码,点击实现波纹效果
1821 0
|
存储 前端开发
仿抖音的音乐旋转
这次是实现一个仿抖音的音乐旋转自定义View,先看一下效果 效果图 实现这个效果主要是采用的拼凑的方法,即先实现音符动画再实现图片旋转动画然后将两个效果合并到一起。
1458 0
|
Android开发
仿抖音底部导航(二)
继续实现仿抖音底部导航 今天要实现效果如下图 实现效果 首先在原基础的布局中加入一个ImageView 这里附上刷新的图片素材 image 然后在原代码中进行修改以实现导航的动画及刷新功能 1.
1742 0
|
Android开发 数据格式 XML
仿抖音注册Dialog实现
今天将仿抖音注册界面完成后接着便将注册界面实现了,这里利用的是Dialog实现的 效果图 接下来介绍如何实现底部弹出Dialog 1.首先编写动画文件 2.
898 0
|
JavaScript 前端开发
第65天:仿网易轮播图
仿网易轮播图 1、HTML部分 1 DOCTYPE html> 2 3 4 5 仿网易轮播图 6 7 8 9 10 11 12 13 14 ...
1232 0
微信小程序把玩(三)tabBar底部导航
原文:微信小程序把玩(三)tabBar底部导航 tabBar相对而言用的还是比较多的,但是用起来并没有难,在app.json中配置下tabBar即可,注意tabBar至少需要两个最多五个Item选项 主要属性: 对于tabBar整体属性设置: ...
950 0
仿淘宝轮播图 ,需要运动框架
DOCTYPE HTML> 无标题文档 #div1 {width: 520px; height: 280px; border: 1px solid #000; margin: 100px auto 0; position: relative; overflow: hidden...
848 0