首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android 分页Title栏滑块成效-ActionBar(模拟网易 腾讯等动态效果)

2012-08-22 
android 分页Title栏滑块效果--ActionBar(模拟网易 腾讯等动态效果)首先我们看几张客户端试图:前两个是网

android 分页Title栏滑块效果--ActionBar(模拟网易 腾讯等动态效果)

首先我们看几张客户端试图:

      android 分页Title栏滑块成效-ActionBar(模拟网易 腾讯等动态效果)    android 分页Title栏滑块成效-ActionBar(模拟网易 腾讯等动态效果)     android 分页Title栏滑块成效-ActionBar(模拟网易 腾讯等动态效果)    android 分页Title栏滑块成效-ActionBar(模拟网易 腾讯等动态效果)

         前两个是网易的,后两个是腾讯的,(注意看上部title分页,当你点击不仅实现了分页,而且背景bar会跟着滑动,这个叫aciotnbar,sdk3.0以后就有了,)看着比一般单存改变背景的效果好看多了.

        代码片段:

 用于描绘.

@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawColor(Color.WHITE);paint.setColor(Color.RED);// 如果当前curRectF=null,也就是第一次访问,则默认为draw第一个barif (curRectF == null)curRectF = new RectF(tv1.getLeft() + space_x, tv1.getTop()- space_y, tv1.getRight() - space_x, tv1.getBottom()+ space_y);// 第一次方位tarRectF=null,默认为drawif (tarRectF == null)tarRectF = new RectF(tv1.getLeft() + space_x, tv1.getTop()- space_y, tv1.getRight() - space_x, tv1.getBottom()+ space_y);// 这个时候需要不停的更新if (Math.abs(curRectF.left - tarRectF.left) < step) {curRectF.left = tarRectF.left;curRectF.right = tarRectF.right;}if (curRectF.left > tarRectF.left) {curRectF.left -= step;curRectF.right -= step;invalidate();// 继续刷新,从而实现滑动效果,每次step32.} else if (curRectF.left < tarRectF.left) {curRectF.left += step;curRectF.right += step;invalidate();}canvas.drawRoundRect(curRectF, 5, 5, paint);}

用于监听点击bar事件.

@Overridepublic void onClick(View v) {tarRectF.left = v.getLeft() + space_x;tarRectF.right = v.getRight() - space_x;tarRectF.top = v.getTop() - space_y;tarRectF.bottom = v.getBottom() + space_y;invalidate();// 刷新}

从而我们就可以实现这个效果:(红色滑块会随着你点击跟着滑动.看起来比较爽.)

android 分页Title栏滑块成效-ActionBar(模拟网易 腾讯等动态效果)     android 分页Title栏滑块成效-ActionBar(模拟网易 腾讯等动态效果)     android 分页Title栏滑块成效-ActionBar(模拟网易 腾讯等动态效果)

现在还有一个问题:那就是我们怎么实现点击下面滑块的时候也让上面view跟着切换呢,你会说用onclick监听,是这样的,不过简单的对下面bar监听,你会发现。滑块并不会跟着滑动,其实原因是你之前调过onclick事件,所以这个时候你再次调用会把以前的冲掉.  不过我们有办法,最重要的是最下面一个actionBar.onClick(v); 

代码片段:

@Overridepublic void onClick(View v) {layout.removeAllViews();Intent intent = null;switch (v.getId()) {case R.id.tv1:intent = new Intent(ActionBarActivity.this, MainActivity1.class);break;case R.id.tv2:intent = new Intent(ActionBarActivity.this, MainActivity2.class);break;case R.id.tv3:intent = new Intent(ActionBarActivity.this, MainActivity3.class);break;case R.id.tv4:intent = new Intent(ActionBarActivity.this, MainActivity4.class);break;default:break;}intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);Window subActivity = getLocalActivityManager().startActivity("subActivity", intent);layout.addView(subActivity.getDecorView(), LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);                                actionBar.onClick(v);                }
这样我们就大功告成了,其实实现起来也不难,我也是参考别人的,相信你也ok的.





        

热点排行