仿抖音底部導航效果(二)

繼續實現仿抖音底部導航

今天要實現效果以下圖

首先在原基礎的佈局中加入一個ImageView

<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" />
    <!--新加入的-->
    <ImageView
        android:id="@+id/reflash"
        android:layout_width="25dp"
        android:src="@mipmap/shuaxin"
        android:visibility="gone"
        android:layout_centerInParent="true"
        android:layout_height="25dp" />
</LinearLayout>

這裏附上刷新的圖片素材android

而後在原代碼中進行修改以實現導航的動畫及刷新功能

1.添加是否顯示刷新圖片的標記ide

//默認設爲flase
private boolean isShowReflashImage=false;
//並添加set方法
public void setShowReflashImage(boolean showReflashImage) {
    isShowReflashImage = showReflashImage;
}

2.而後修改激活和取消激活的方法佈局

public void startActive(){
    //根據上一步添加的標記來判斷是否顯示ImageView
    if(isShowReflashImage){
        textView_title.setVisibility(GONE);
        imageView_Shuaxin.setVisibility(VISIBLE);
    }else {
        textView_title.setTextColor(Color.WHITE);
        textView_title.setTextSize(18);
    }
    textView_line.animate().alpha(1).setDuration(200).start();
}
public void cancelActive(){
    if(isShowReflashImage){
        textView_title.setVisibility(VISIBLE);
        imageView_Shuaxin.setVisibility(GONE);
        textView_line.setAlpha(0);
    }else {
        textView_title.setTextColor(Color.parseColor("#F1F1F1"));
        textView_title.setTextSize(16);
    }
    textView_line.animate().alpha(0).setDuration(200).start();
}

3.給負責刷新的ImageView添加旋轉動畫動畫

private void initListener() {
    final Animation rotateAnimation = new RotateAnimation(0,-360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
    rotateAnimation.setDuration(300);
    imageView_Shuaxin.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            imageView_Shuaxin.startAnimation(rotateAnimation);
        }
    });
}

4.最後加上刷新事件this

1)新建NavItemReflashListener3d

public interface NavItemReflashListener {
    void onReflash(View v);
}

2)給NavItemView加上NavItemReflashListener屬性並設置set方法code

public void setNavItemReflashListener(NavItemReflashListener navItemReflashListener) {
    this.navItemReflashListener = navItemReflashListener;
}

3)在imageView_Shuaxin的點擊事件中調用onReflash(View v)方法事件

imageView_Shuaxin.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            imageView_Shuaxin.startAnimation(rotateAnimation);
            if(navItemReflashListener!=null){
                navItemReflashListener.onReflash(v);
            }
        }
});

5.在MainActivity中將想要顯示刷新控件的NavItemView設置ShowReflashImagewei爲true並加上刷新事件圖片

navItemView1.setShowReflashImage(true);
    navItemView1.setNavItemReflashListener(new NavItemReflashListener() {
        @Override
        public void onReflash(View v) {
            Toast.makeText(MainActivity.this, "刷新", Toast.LENGTH_SHORT).show();
        }
});

若是須要完整的能夠評論哈~ip

相關文章
相關標籤/搜索