当前位置: 移动技术网 > 移动技术>移动开发>Android > Android仿网易客户端顶部导航栏效果

Android仿网易客户端顶部导航栏效果

2019年07月24日  | 移动技术网移动技术  | 我要评论

最近刚写了一个网易客户端首页导航条的动画效果,现在分享出来给大家学习学习。我说一下这个效果的核心原理。下面是效果图:

       

 首先是布局,这个布局是我从网易客户端反编译后弄来的。大家看后应该明白,布局文件如下:

<framelayout 
    android:id="@id/column_navi" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/top_column_bg" > 
 
    <horizontalscrollview 
      android:id="@id/column_scrollview" 
      android:layout_width="fill_parent" 
      android:layout_height="45.0dip" 
      android:layout_gravity="center" 
      android:fadingedge="vertical" 
      android:paddingleft="9.0dip" 
      android:paddingright="9.0dip" 
      android:scrollbars="none" > 
 
      <framelayout 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" > 
 
        <imageview 
          android:id="@id/column_slide_bar" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center_vertical" 
          android:src="@drawable/slidebar" /> 
 
        <linearlayout 
          android:id="@id/column_title_layout" 
          android:layout_width="wrap_content" 
          android:layout_height="fill_parent" 
          android:layout_gravity="center_vertical" 
          android:gravity="center_vertical" 
          android:paddingleft="5px" 
          android:weightsum="6.0" /> 
      </framelayout> 
    </horizontalscrollview> 
 
    <imagebutton 
      android:id="@id/column_to_left" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignparentleft="true" 
      android:layout_centervertical="true" 
      android:layout_gravity="left|center" 
      android:layout_marginleft="2.0dip" 
      android:layout_marginright="1.0dip" 
      android:background="#00000000" 
      android:src="@drawable/arr_left" 
      android:visibility="visible" /> 
 
    <imagebutton 
      android:id="@id/column_to_right" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignparentright="true" 
      android:layout_centervertical="true" 
      android:layout_gravity="right|center" 
      android:layout_marginleft="1.0dip" 
      android:layout_marginright="2.0dip" 
      android:background="#00000000" 
      android:src="@drawable/arr_right" 
      android:visibility="visible" /> 
  </framelayout> 

这里用了horizontalscrollview横向滚动视图主要是为了实现当导航栏个数超出屏幕以后可以实现左右移动的效果,这2个imagebutton则是用来实现左右滚动的操作。horizontalscrollview里面用的一个框架布局,大家都知道框架布局是一个叠加式的 布局,所以里面的imageview会在linearlayout布局下面一层,这个imageview就是实现动态背景效果的。而linearlayout里面放的是textview,这里是在后台程序里面动态添加。

那要怎样实现当我点击一个textview 后实现后面的imageview动态移动到我选中的textview位置呢?这里我们需要为每一个textview添加ontouchevent()时间,并且监听action_down时间,也就是手指按下的时候,这时我们就启动一个translateanimation平移动画,在动画结束时,再将imageview移动到textview的位置。移动textview的位置我这里是动态调整textview的布局来实现的。

下面是实现的代码:

private void translateimage(motionevent event) { 
    float x = event.getx(); 
    float rx = event.getrawx(); 
    final float nx = rx - x - 12; 
    translateanimation trans = null; 
    if (nx > lastx) { 
      trans = new translateanimation(0, nx - lastx, 0, 0); 
    } else if (nx < lastx) { 
      trans = new translateanimation(0, (lastx - nx) * -1, 0, 0); 
    } else { 
      return; 
    } 
    trans.setduration(300); 
 
    trans.setanimationlistener(new animationlistener() { 
 
      @override 
      public void onanimationstart(animation animation) { 
        // todo auto-generated method stub 
 
      } 
 
      @override 
      public void onanimationrepeat(animation animation) { 
        // todo auto-generated method stub 
 
      } 
 
      @override 
      public void onanimationend(animation animation) { 
        framelayout.layoutparams params = (android.widget.framelayout.layoutparams) column_slide_bar 
            .getlayoutparams(); 
        params.leftmargin = (int) nx; 
        column_slide_bar.setlayoutparams(params); 
      } 
    }); 
    trans.setfillenabled(true); 
    column_slide_bar.startanimation(trans); 
    lastx = (int) nx; 
  } 

这个方法的开头我是取到手指按下的textview的坐标位置,而lastx是上一次手指按下的位置,我这里做了判断来确定移动的方向,然后给动画添加了一个动画监听事件,在动画结束时我就动态的把imageview移动到新的坐标位置。setfillenabled(true);这里的作用主要是避免动画乱跳,这里具体是什么原因我也还不太清楚,但是设置以后动画一切都正常。

下面是textview的ontouchevent事件的代码:

@override 
  public boolean ontouch(view v, motionevent event) { 
    if (event.getaction() == motionevent.action_down) { 
      if (up_text != null) { 
        up_text.settextcolor(color.black); 
      } else { 
        textview text = (textview) context 
            .findviewbyid(r.id.head_lines); 
        text.settextcolor(color.black); 
      } 
      translateimage(event); 
      textview tv = (textview) v; 
      tv.settextcolor(color.white); 
      up_text = tv; 
    } 
    return true; 
  } 

 在这段代码中我主要是实现了textview的字体颜色的变还,大家应该看得懂,没什么好说的吧。

最后就是实现horizontalscrollview控件通过单机左右的imagebutton来实现左右移动,这个就是在imagebutton的onclick事件中来调用horizontalscrollview的smoothscrollto(x,y)方法这里面是传入新的坐标。下面是实现代码:

private void addlistener() { 
  column_to_left.setonclicklistener(new onclicklistener() { 
 
    @override 
    public void onclick(view v) { 
      column_scrollview.smoothscrollto( 
          column_scrollview.getscrollx() - 40, 0); 
    } 
  }); 
  column_to_right.setonclicklistener(new view.onclicklistener() { 
 
    @override 
    public void onclick(view v) { 
      column_scrollview.smoothscrollto( 
          column_scrollview.getscrollx() + 40, 0); 
    } 
  }); 
} 

下面是动态添加textview的代码:

private void initview() { 
  column_title_layout = (linearlayout) findviewbyid(r.id.column_title_layout); 
  column_scrollview = (horizontalscrollview) findviewbyid(r.id.column_scrollview); 
  column_slide_bar = (imageview) findviewbyid(r.id.column_slide_bar); 
  column_to_left = (imagebutton) findviewbyid(r.id.column_to_left); 
  column_to_right = (imagebutton) findviewbyid(r.id.column_to_right); 
 
  linearlayout.layoutparams params = new linearlayout.layoutparams(65, 
      layoutparams.wrap_content); 
  params.gravity = gravity.center_horizontal | gravity.center_vertical; 
  params.leftmargin = 9; 
 
  textviewontouchlistener listener = new textviewontouchlistener( 
      column_slide_bar, this); 
  textview text = null; 
  for (int i = 0; i < 6; i++) { 
    text = new textview(this); 
    text.settextsize(16); 
    switch (i) { 
    case 0: 
      text.setid(r.id.head_lines); 
      text.settextcolor(color.white); 
      text.settext("头条"); 
      break; 
    case 1: 
      text.setid(r.id.sport); 
      text.settextcolor(color.black); 
      text.settext("体育"); 
      break; 
    case 2: 
      text.setid(r.id.entertainment); 
      text.settextcolor(color.black); 
      text.settext("娱乐"); 
      break; 
    case 3: 
      text.setid(r.id.finance); 
      text.settextcolor(color.black); 
      text.settext("财经"); 
      break; 
    case 4: 
      text.setid(r.id.technology); 
      text.settextcolor(color.black); 
      text.settext("科技"); 
      break; 
    case 5: 
      text.setid(r.id.more); 
      text.settextcolor(color.black); 
      text.settext("更多"); 
      break; 
    } 
    text.setontouchlistener(listener); 
    column_title_layout.addview(text, params); 
  } 
} 

下面是ids.xml文件中定义的动态生成控件的id:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 
  <item name="column_scrollview" type="id"/> 
  <item name="column_slide_bar" type="id"/> 
  <item name="column_title_layout" type="id"/> 
  <item name="column_navi" type="id"/> 
  <item name="column_to_left" type="id"/> 
  <item name="column_to_right" type="id"/> 
  <item name="scroll_layout" type="id"/> 
  <item name="vote" type="id"/> 
  <item name="comment" type="id"/> 
  <item name="picture" type="id"/> 
  <item name="topic" type="id"/> 
  <item name="news" type="id"/> 
  <item name="head_lines" type="id"/> 
  <item name="sport" type="id"/> 
  <item name="entertainment" type="id"/> 
  <item name="finance" type="id"/> 
  <item name="technology" type="id"/> 
  <item name="more" type="id"/> 
 
</resources> 

源码下载:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网