Android基于2.3 底部平滑导航

 

 

今天给大家分享一个实现底部背景图片平滑切换动画效果

来看看主要代码:

 

  
  
  
  
  1. public void init(){ 
  2.         //tabHost = (TabHost) this.findViewById(R.id.tabHost); 
  3.         tabHost = this.getTabHost(); 
  4.         TabSpec indexTab = tabHost.newTabSpec("index").setIndicator("index"); 
  5.         indexTab.setContent(new Intent(this, IndexActivity.class)); 
  6.         tabHost.addTab(indexTab); 
  7.          
  8.         TabSpec messageTab = tabHost.newTabSpec("message").setIndicator("message"); 
  9.         messageTab.setContent(new Intent(this, MessageActivity.class)); 
  10.         tabHost.addTab(messageTab); 
  11.          
  12.         TabSpec newTab = tabHost.newTabSpec("new").setIndicator("new"); 
  13.         newTab.setContent(new Intent(this, NewActivity.class)); 
  14.         tabHost.addTab(newTab); 
  15.          
  16.         TabSpec goodTab = tabHost.newTabSpec("good").setIndicator("good"); 
  17.         goodTab.setContent(new Intent(this, GoodActivity.class)); 
  18.         tabHost.addTab(goodTab); 
  19.          
  20.         tabHost.setCurrentTabByTag("index"); 
  21.          
  22.         layout_bottom = (RelativeLayout) this.findViewById(R.id.layout_bottom); 
  23.         layout_index = (RelativeLayout) this.findViewById(R.id.layout_index); 
  24.         layout_message = (RelativeLayout) this.findViewById(R.id.layout_message); 
  25.         layout_new = (RelativeLayout) this.findViewById(R.id.layout_new); 
  26.         layout_good = (RelativeLayout) this.findViewById(R.id.layout_good); 
  27.         tv_index = (TextView) this.findViewById(R.id.tv_index); 
  28.         tv_message = (TextView) this.findViewById(R.id.tv_message); 
  29.         tv_new = (TextView) this.findViewById(R.id.tv_new); 
  30.         tv_good = (TextView) this.findViewById(R.id.tv_good); 
  31.          
  32.         layout_index.setOnClickListener(this); 
  33.         layout_message.setOnClickListener(this); 
  34.         layout_new.setOnClickListener(this); 
  35.         layout_good.setOnClickListener(this); 
  36.          
  37.         ImageView backgroud = new ImageView(this); 
  38.         backgroud.setTag("default"); 
  39.         backgroud.setImageResource(R.drawable.bottom_backgroud); //设置选中Tab的背景图片
  40.          
  41.          
  42.         RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
  43.         rl.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); //设置特性
  44.          
  45.         switch (mCurrentTab) { 
  46.         case 0
  47.             layout_index.addView(backgroud, 0, rl); 
  48.             break
  49.         case 1
  50.             layout_message.addView(backgroud, 0, rl); 
  51.             break
  52.         case 2
  53.             layout_new.addView(backgroud, 0, rl); 
  54.             break
  55.         case 3
  56.             layout_good.addView(backgroud, 0, rl); 
  57.             break
  58.  
  59.         default
  60.             break
  61.         } 
  62.          
  63.     } 
  64.      
  65.     //替换背景 
  66.     public void replace(){ 
  67.         switch (mCurrentTab) { 
  68.         case 0
  69.             toggleTop(layout_index); 
  70.             break
  71.         case 1
  72.             toggleTop(layout_message); 
  73.             break
  74.         case 2
  75.             toggleTop(layout_new); 
  76.             break
  77.         case 3
  78.             toggleTop(layout_good); 
  79.             break
  80.  
  81.         default
  82.             break
  83.         } 
  84.     } 
  85.      
  86.     //切换背景图片 
  87.     public void toggleTop(RelativeLayout relativeLayout ){ 
  88.         ImageView tag = (ImageView) relativeLayout.findViewWithTag("default"); 
  89.         mWidth = tag.getWidth();//获取背景背景的宽 
  90.         mHeight = tag.getHeight();//获取背景背景的高 
  91.          
  92.         RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(mWidth, mHeight); 
  93.         rl.addRule(RelativeLayout.CENTER_VERTICAL);//设置布局垂直居中 
  94.         rl.leftMargin = tag.getLeft()+ ((RelativeLayout) tag.getParent()).getLeft();//设置左边距 
  95.         rl.topMargin = tag.getTop()+ ((RelativeLayout) tag.getParent()).getTop();//设置顶边距 
  96.          
  97.         // 获取起始位置 
  98.         firstLeft = tag.getLeft()+ ((RelativeLayout) tag.getParent()).getLeft(); 
  99.          
  100.         ImageView iv = new ImageView(this); 
  101.         iv.setTag("move"); 
  102.         iv.setImageResource(R.drawable.bottom_backgroud);//设置平滑移动时的背景图片 
  103.          
  104.         layout_bottom.addView(iv, 0, rl);//把rl添加到是外面的RelativeLayout中 
  105.         relativeLayout.removeView(tag); 
  106.     } 
  107.  
  108.     //移动Tab 
  109.     public void moveToTab(int position){ 
  110.         if(!isTag){ 
  111.             replace(); // 初次使用移除TAG添加新的top_select为RelativeLayout所使用 
  112.             isTag = true
  113.         } 
  114.          
  115.         iv_tag = (ImageView) layout_bottom.findViewWithTag("move"); 
  116.         int tabLeft; 
  117.         int endLeft = 0
  118.         boolean run = false
  119.         switch (position) { 
  120.         case 0
  121.             if(mCurrentTab != 0){ 
  122.                 // 中心位置 
  123.                 tabLeft = ((RelativeLayout) tv_index.getParent()).getLeft()+ tv_index.getLeft() + tv_index.getWidth() / 2
  124.                 // 最终位置 
  125.                 endLeft = tabLeft - mWidth / 2
  126.                 mCurrentTab = 0
  127.                 tabHost.setCurrentTabByTag("index"); 
  128.                 run = true
  129.             } 
  130.             break
  131.         case 1
  132.             if(mCurrentTab != 1){ 
  133.                 if (mCurrentTab != 1) { 
  134.                     tabLeft = ((RelativeLayout) tv_message.getParent()).getLeft() + tv_message.getLeft() + tv_message.getWidth() / 2
  135.                     endLeft = tabLeft - mWidth / 2
  136.                     tabHost.setCurrentTabByTag("message"); 
  137.                     mCurrentTab = 1
  138.                     run = true
  139.                 } 
  140.             }    
  141.             break
  142.         case 2
  143.             if(mCurrentTab != 2){ 
  144.                 tabLeft = ((RelativeLayout)tv_new.getParent()).getLeft()+ tv_new.getLeft()+ tv_new.getWidth() / 2
  145.                 endLeft = tabLeft - mWidth / 2
  146.                 mCurrentTab = 2
  147.                 tabHost.setCurrentTabByTag("new"); 
  148.                 run = true
  149.             } 
  150.             break
  151.         case 3
  152.             if(mCurrentTab != 3){ 
  153.                 tabLeft = ((RelativeLayout) tv_good.getParent()).getLeft()+ tv_good.getLeft() + tv_good.getWidth() / 2
  154.                 endLeft = tabLeft - mWidth / 2
  155.                 mCurrentTab = 3
  156.                 tabHost.setCurrentTabByTag("good"); 
  157.                 run = true
  158.             } 
  159.             break
  160.         default
  161.             break
  162.         } 
  163.         if (run) { 
  164.             TranslateAnimation animation = new TranslateAnimation(startLeft,endLeft - firstLeft, 00); 
  165.             startLeft = endLeft - firstLeft; // 重新设定起始位置 
  166.             animation.setDuration(400); 
  167.             animation.setFillAfter(true); 
  168.             iv_tag.startAnimation(animation); 
  169.         } 
  170.          
  171.     } 
  172.  
  173. @Override 
  174.     public void onClick(View v) { 
  175.         switch (v.getId()) { 
  176.         case R.id.layout_index: 
  177.             moveToTab(0); 
  178.             break
  179.         case R.id.layout_message: 
  180.             moveToTab(1); 
  181.             break
  182.         case R.id.layout_new: 
  183.             moveToTab(2); 
  184.             break
  185.         case R.id.layout_good: 
  186.             moveToTab(3); 
  187.             break
  188.         default
  189.             break
  190.         } 
  191.          
  192.     } 

附件以上传

你可能感兴趣的:(android,activty)