Android 分页Title栏滑块效果--ActionBar(模拟网易 腾讯等动态效果) 首先我们看几张客户端试图:前两个是网易的,后两个是腾讯的,(注意看上部title分页,当你点击不仅实现了分页,而且背景bar会跟着滑动,这个叫aciotnbar,sdk3.0以后就有了,)看着比一般单存改变背景的效果好看多了.代码片段:用于描绘.
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- canvas.drawColor(Color.WHITE);
- paint.setColor(Color.RED);
- // 如果当前curRectF=null,也就是第一次访问,则默认为draw第一个bar
- if (curRectF == null)
- curRectF = new RectF(tv1.getLeft() + space_x, tv1.getTop()
- - space_y, tv1.getRight() - space_x, tv1.getBottom()
- + space_y);
- // 第一次方位tarRectF=null,默认为draw
- if (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事件.
- @Override
- public 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();// 刷新
- }