歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 分頁Title欄滑塊效果--ActionBar(模擬網易 騰訊等動態效果)

Android 分頁Title欄滑塊效果--ActionBar(模擬網易 騰訊等動態效果)

日期:2017/3/1 10:11:34   编辑:Linux編程

Android 分頁Title欄滑塊效果--ActionBar(模擬網易 騰訊等動態效果)

首先我們看幾張客戶端試圖:

前兩個是網易的,後兩個是騰訊的,(注意看上部title分頁,當你點擊不僅實現了分頁,而且背景bar會跟著滑動,這個叫aciotnbar,sdk3.0以後就有了,)看著比一般單存改變背景的效果好看多了.

代碼片段:

用於描繪.

  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. super.onDraw(canvas);
  4. canvas.drawColor(Color.WHITE);
  5. paint.setColor(Color.RED);
  6. // 如果當前curRectF=null,也就是第一次訪問,則默認為draw第一個bar
  7. if (curRectF == null)
  8. curRectF = new RectF(tv1.getLeft() + space_x, tv1.getTop()
  9. - space_y, tv1.getRight() - space_x, tv1.getBottom()
  10. + space_y);
  11. // 第一次方位tarRectF=null,默認為draw
  12. if (tarRectF == null)
  13. tarRectF = new RectF(tv1.getLeft() + space_x, tv1.getTop()
  14. - space_y, tv1.getRight() - space_x, tv1.getBottom()
  15. + space_y);
  16. // 這個時候需要不停的更新
  17. if (Math.abs(curRectF.left - tarRectF.left) < step) {
  18. curRectF.left = tarRectF.left;
  19. curRectF.right = tarRectF.right;
  20. }
  21. if (curRectF.left > tarRectF.left) {
  22. curRectF.left -= step;
  23. curRectF.right -= step;
  24. invalidate();// 繼續刷新,從而實現滑動效果,每次step32.
  25. } else if (curRectF.left < tarRectF.left) {
  26. curRectF.left += step;
  27. curRectF.right += step;
  28. invalidate();
  29. }
  30. canvas.drawRoundRect(curRectF, 5, 5, paint);
  31. }

用於監聽點擊bar事件.

  1. @Override
  2. public void onClick(View v) {
  3. tarRectF.left = v.getLeft() + space_x;
  4. tarRectF.right = v.getRight() - space_x;
  5. tarRectF.top = v.getTop() - space_y;
  6. tarRectF.bottom = v.getBottom() + space_y;
  7. invalidate();// 刷新
  8. }
Copyright © Linux教程網 All Rights Reserved