/packages/SystemUI/src/com/android/systemui/recent/FadedEdgeDrawHelper.java

https://github.com/aizuzi/platform_frameworks_base · Java · 189 lines · 140 code · 26 blank · 23 comment · 26 complexity · 13313fb1061da69c3838054650653b6a MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.android.systemui.recent;
  17. import android.content.Context;
  18. import android.content.res.TypedArray;
  19. import android.graphics.Canvas;
  20. import android.graphics.LinearGradient;
  21. import android.graphics.Matrix;
  22. import android.graphics.Paint;
  23. import android.graphics.Shader;
  24. import android.util.AttributeSet;
  25. import android.view.View;
  26. import android.view.ViewConfiguration;
  27. import android.widget.LinearLayout;
  28. import com.android.systemui.R;
  29. public class FadedEdgeDrawHelper {
  30. public static final boolean OPTIMIZE_SW_RENDERED_RECENTS = true;
  31. public static final boolean USE_DARK_FADE_IN_HW_ACCELERATED_MODE = true;
  32. private View mScrollView;
  33. private int mFadingEdgeLength;
  34. private boolean mIsVertical;
  35. private boolean mSoftwareRendered = false;
  36. private Paint mBlackPaint;
  37. private Paint mFadePaint;
  38. private Matrix mFadeMatrix;
  39. private LinearGradient mFade;
  40. public static FadedEdgeDrawHelper create(Context context,
  41. AttributeSet attrs, View scrollView, boolean isVertical) {
  42. boolean isTablet = context.getResources().
  43. getBoolean(R.bool.config_recents_interface_for_tablets);
  44. if (!isTablet && (OPTIMIZE_SW_RENDERED_RECENTS || USE_DARK_FADE_IN_HW_ACCELERATED_MODE)) {
  45. return new FadedEdgeDrawHelper(context, attrs, scrollView, isVertical);
  46. } else {
  47. return null;
  48. }
  49. }
  50. public FadedEdgeDrawHelper(Context context,
  51. AttributeSet attrs, View scrollView, boolean isVertical) {
  52. mScrollView = scrollView;
  53. TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View);
  54. mFadingEdgeLength = a.getDimensionPixelSize(android.R.styleable.View_fadingEdgeLength,
  55. ViewConfiguration.get(context).getScaledFadingEdgeLength());
  56. mIsVertical = isVertical;
  57. }
  58. public void onAttachedToWindowCallback(
  59. LinearLayout layout, boolean hardwareAccelerated) {
  60. mSoftwareRendered = !hardwareAccelerated;
  61. if ((mSoftwareRendered && OPTIMIZE_SW_RENDERED_RECENTS)
  62. || USE_DARK_FADE_IN_HW_ACCELERATED_MODE) {
  63. mScrollView.setVerticalFadingEdgeEnabled(false);
  64. mScrollView.setHorizontalFadingEdgeEnabled(false);
  65. }
  66. }
  67. public void addViewCallback(View newLinearLayoutChild) {
  68. if (mSoftwareRendered && OPTIMIZE_SW_RENDERED_RECENTS) {
  69. final RecentsPanelView.ViewHolder holder =
  70. (RecentsPanelView.ViewHolder) newLinearLayoutChild.getTag();
  71. holder.labelView.setDrawingCacheEnabled(true);
  72. holder.labelView.buildDrawingCache();
  73. }
  74. }
  75. public void drawCallback(Canvas canvas,
  76. int left, int right, int top, int bottom, int scrollX, int scrollY,
  77. float topFadingEdgeStrength, float bottomFadingEdgeStrength,
  78. float leftFadingEdgeStrength, float rightFadingEdgeStrength, int mPaddingTop) {
  79. if ((mSoftwareRendered && OPTIMIZE_SW_RENDERED_RECENTS)
  80. || USE_DARK_FADE_IN_HW_ACCELERATED_MODE) {
  81. if (mFadePaint == null) {
  82. mFadePaint = new Paint();
  83. mFadeMatrix = new Matrix();
  84. // use use a height of 1, and then wack the matrix each time we
  85. // actually use it.
  86. mFade = new LinearGradient(0, 0, 0, 1, 0xCC000000, 0, Shader.TileMode.CLAMP);
  87. // PULL OUT THIS CONSTANT
  88. mFadePaint.setShader(mFade);
  89. }
  90. // draw the fade effect
  91. boolean drawTop = false;
  92. boolean drawBottom = false;
  93. boolean drawLeft = false;
  94. boolean drawRight = false;
  95. float topFadeStrength = 0.0f;
  96. float bottomFadeStrength = 0.0f;
  97. float leftFadeStrength = 0.0f;
  98. float rightFadeStrength = 0.0f;
  99. final float fadeHeight = mFadingEdgeLength;
  100. int length = (int) fadeHeight;
  101. // clip the fade length if top and bottom fades overlap
  102. // overlapping fades produce odd-looking artifacts
  103. if (mIsVertical && (top + length > bottom - length)) {
  104. length = (bottom - top) / 2;
  105. }
  106. // also clip horizontal fades if necessary
  107. if (!mIsVertical && (left + length > right - length)) {
  108. length = (right - left) / 2;
  109. }
  110. if (mIsVertical) {
  111. topFadeStrength = Math.max(0.0f, Math.min(1.0f, topFadingEdgeStrength));
  112. drawTop = topFadeStrength * fadeHeight > 1.0f;
  113. bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, bottomFadingEdgeStrength));
  114. drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
  115. }
  116. if (!mIsVertical) {
  117. leftFadeStrength = Math.max(0.0f, Math.min(1.0f, leftFadingEdgeStrength));
  118. drawLeft = leftFadeStrength * fadeHeight > 1.0f;
  119. rightFadeStrength = Math.max(0.0f, Math.min(1.0f, rightFadingEdgeStrength));
  120. drawRight = rightFadeStrength * fadeHeight > 1.0f;
  121. }
  122. if (drawTop) {
  123. mFadeMatrix.setScale(1, fadeHeight * topFadeStrength);
  124. mFadeMatrix.postTranslate(left, top);
  125. mFade.setLocalMatrix(mFadeMatrix);
  126. canvas.drawRect(left, top, right, top + length, mFadePaint);
  127. if (mBlackPaint == null) {
  128. // Draw under the status bar at the top
  129. mBlackPaint = new Paint();
  130. mBlackPaint.setColor(0xFF000000);
  131. }
  132. canvas.drawRect(left, top - mPaddingTop, right, top, mBlackPaint);
  133. }
  134. if (drawBottom) {
  135. mFadeMatrix.setScale(1, fadeHeight * bottomFadeStrength);
  136. mFadeMatrix.postRotate(180);
  137. mFadeMatrix.postTranslate(left, bottom);
  138. mFade.setLocalMatrix(mFadeMatrix);
  139. canvas.drawRect(left, bottom - length, right, bottom, mFadePaint);
  140. }
  141. if (drawLeft) {
  142. mFadeMatrix.setScale(1, fadeHeight * leftFadeStrength);
  143. mFadeMatrix.postRotate(-90);
  144. mFadeMatrix.postTranslate(left, top);
  145. mFade.setLocalMatrix(mFadeMatrix);
  146. canvas.drawRect(left, top, left + length, bottom, mFadePaint);
  147. }
  148. if (drawRight) {
  149. mFadeMatrix.setScale(1, fadeHeight * rightFadeStrength);
  150. mFadeMatrix.postRotate(90);
  151. mFadeMatrix.postTranslate(right, top);
  152. mFade.setLocalMatrix(mFadeMatrix);
  153. canvas.drawRect(right - length, top, right, bottom, mFadePaint);
  154. }
  155. }
  156. }
  157. public int getVerticalFadingEdgeLength() {
  158. return mFadingEdgeLength;
  159. }
  160. public int getHorizontalFadingEdgeLength() {
  161. return mFadingEdgeLength;
  162. }
  163. }