From abee8a15b991721c2dd812ddda404919c078dbd6 Mon Sep 17 00:00:00 2001 From: tanwenkang Date: Thu, 17 Oct 2019 13:25:07 +0800 Subject: [PATCH] Opt: Reuse constant object. --- .../slideback/support/SlideLayout.java | 30 +++++++++---------- .../slideback/support/SlideManager.java | 5 +++- .../support/utility/ViewDragHelper.java | 4 +-- .../src/main/res/values/attrs.xml | 2 +- .../src/main/res/values/styles.xml | 2 +- .../com/phantomvk/slideback/SlideLayout.java | 30 +++++++++---------- .../com/phantomvk/slideback/SlideManager.java | 5 +++- .../slideback/utility/ViewDragHelper.java | 4 +-- slideback/src/main/res/values/attrs.xml | 2 +- slideback/src/main/res/values/styles.xml | 2 +- 10 files changed, 46 insertions(+), 40 deletions(-) diff --git a/slideback-support/src/main/java/com/phantomvk/slideback/support/SlideLayout.java b/slideback-support/src/main/java/com/phantomvk/slideback/support/SlideLayout.java index ec69364..5f7f87e 100644 --- a/slideback-support/src/main/java/com/phantomvk/slideback/support/SlideLayout.java +++ b/slideback-support/src/main/java/com/phantomvk/slideback/support/SlideLayout.java @@ -55,6 +55,11 @@ public class SlideLayout extends FrameLayout { */ private static final int SLIDE_OVER_RANGE = 3; + /** + * Rect for drawing shadow. + */ + private static final Rect RECT = new Rect(); + /** * ViewDragHelper */ @@ -135,11 +140,6 @@ public class SlideLayout extends FrameLayout { */ private int mViewTop; - /** - * Rect for drawing shadow. - */ - private final Rect mRect = new Rect(); - /** * The target activity. */ @@ -391,28 +391,28 @@ private void drawScrim(Canvas canvas, View child) { } private void drawShadow(Canvas canvas, View child) { - child.getHitRect(mRect); + child.getHitRect(RECT); final int alpha = (int) (mShadowOpacity * FULL_ALPHA); if ((mEdge & EDGE_LEFT) != 0) { - mShadowLeft.setBounds(mRect.left - mShadowLeft.getIntrinsicWidth(), mRect.top, - mRect.left, mRect.bottom); + mShadowLeft.setBounds(RECT.left - mShadowLeft.getIntrinsicWidth(), RECT.top, + RECT.left, RECT.bottom); mShadowLeft.setAlpha(alpha); mShadowLeft.draw(canvas); } else if ((mEdge & EDGE_RIGHT) != 0) { - mShadowRight.setBounds(mRect.right, mRect.top, - mRect.right + mShadowRight.getIntrinsicWidth(), mRect.bottom); + mShadowRight.setBounds(RECT.right, RECT.top, + RECT.right + mShadowRight.getIntrinsicWidth(), RECT.bottom); mShadowRight.setAlpha(alpha); mShadowRight.draw(canvas); } else if ((mEdge & EDGE_TOP) != 0) { - mShadowTop.setBounds(mRect.left, mRect.top - mShadowTop.getIntrinsicHeight(), - mRect.right, mRect.top); + mShadowTop.setBounds(RECT.left, RECT.top - mShadowTop.getIntrinsicHeight(), + RECT.right, RECT.top); mShadowTop.setAlpha(alpha); mShadowTop.draw(canvas); } else if ((mEdge & EDGE_BOTTOM) != 0) { - mShadowBottom.setBounds(mRect.left, mRect.bottom, mRect.right, - mRect.bottom + mShadowBottom.getIntrinsicHeight()); + mShadowBottom.setBounds(RECT.left, RECT.bottom, RECT.right, + RECT.bottom + mShadowBottom.getIntrinsicHeight()); mShadowBottom.setAlpha(alpha); mShadowBottom.draw(canvas); } @@ -697,7 +697,7 @@ public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) { } @Override - public boolean isMoveActionValid() { + public boolean isValidMoveAction() { return mDrawComplete && mEnterAnimationComplete; } } diff --git a/slideback-support/src/main/java/com/phantomvk/slideback/support/SlideManager.java b/slideback-support/src/main/java/com/phantomvk/slideback/support/SlideManager.java index 2069e7d..d69492d 100644 --- a/slideback-support/src/main/java/com/phantomvk/slideback/support/SlideManager.java +++ b/slideback-support/src/main/java/com/phantomvk/slideback/support/SlideManager.java @@ -14,6 +14,9 @@ import com.phantomvk.slideback.support.listener.SlideStateListener; public class SlideManager { + + private static final ColorDrawable DRAWABLE_TRANSPARENT = new ColorDrawable(Color.TRANSPARENT); + /** * The target activity to control. */ @@ -54,7 +57,7 @@ public SlideManager(@NonNull Activity activity, @Nullable SlideStateListener lis slideLayout = new SlideLayout(activity); slideLayout.addListener((listener == null) ? new SlideStateAdapter(activity) : listener); - activity.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); + activity.getWindow().setBackgroundDrawable(DRAWABLE_TRANSPARENT); } /** diff --git a/slideback-support/src/main/java/com/phantomvk/slideback/support/utility/ViewDragHelper.java b/slideback-support/src/main/java/com/phantomvk/slideback/support/utility/ViewDragHelper.java index f626009..b4029c9 100644 --- a/slideback-support/src/main/java/com/phantomvk/slideback/support/utility/ViewDragHelper.java +++ b/slideback-support/src/main/java/com/phantomvk/slideback/support/utility/ViewDragHelper.java @@ -331,7 +331,7 @@ public int clampViewPositionVertical(@NonNull View child, int top, int dy) { * and enter animation are both completed in target activity. The extending * class must override this method and provide the flag. */ - public boolean isMoveActionValid() { + public boolean isValidMoveAction() { return false; } } @@ -1204,7 +1204,7 @@ public void processTouchEvent(@NonNull MotionEvent ev) { if (mDragState == STATE_DRAGGING) { // If pointer is invalid then skip the ACTION_MOVE. if (!isValidPointerForActionMove(mActivePointerId)) break; - if (!mCallback.isMoveActionValid()) break; + if (!mCallback.isValidMoveAction()) break; final int index = ev.findPointerIndex(mActivePointerId); final float x = ev.getX(index); diff --git a/slideback-support/src/main/res/values/attrs.xml b/slideback-support/src/main/res/values/attrs.xml index 16a145b..f69e5c0 100644 --- a/slideback-support/src/main/res/values/attrs.xml +++ b/slideback-support/src/main/res/values/attrs.xml @@ -15,4 +15,4 @@ - \ No newline at end of file + diff --git a/slideback-support/src/main/res/values/styles.xml b/slideback-support/src/main/res/values/styles.xml index 140cd89..ed49413 100644 --- a/slideback-support/src/main/res/values/styles.xml +++ b/slideback-support/src/main/res/values/styles.xml @@ -8,4 +8,4 @@ @drawable/drawable_slide_back_top @drawable/drawable_slide_back_bottom - \ No newline at end of file + diff --git a/slideback/src/main/java/com/phantomvk/slideback/SlideLayout.java b/slideback/src/main/java/com/phantomvk/slideback/SlideLayout.java index 2c2970f..f45eb09 100644 --- a/slideback/src/main/java/com/phantomvk/slideback/SlideLayout.java +++ b/slideback/src/main/java/com/phantomvk/slideback/SlideLayout.java @@ -56,6 +56,11 @@ public class SlideLayout extends FrameLayout { */ private static final int SLIDE_OVER_RANGE = 3; + /** + * Rect for drawing shadow. + */ + private static final Rect RECT = new Rect(); + /** * ViewDragHelper */ @@ -136,11 +141,6 @@ public class SlideLayout extends FrameLayout { */ private int mViewTop; - /** - * Rect for drawing shadow. - */ - private final Rect mRect = new Rect(); - /** * The target activity. */ @@ -392,28 +392,28 @@ private void drawScrim(Canvas canvas, View child) { } private void drawShadow(Canvas canvas, View child) { - child.getHitRect(mRect); + child.getHitRect(RECT); final int alpha = (int) (mShadowOpacity * FULL_ALPHA); if ((mEdge & EDGE_LEFT) != 0) { - mShadowLeft.setBounds(mRect.left - mShadowLeft.getIntrinsicWidth(), mRect.top, - mRect.left, mRect.bottom); + mShadowLeft.setBounds(RECT.left - mShadowLeft.getIntrinsicWidth(), RECT.top, + RECT.left, RECT.bottom); mShadowLeft.setAlpha(alpha); mShadowLeft.draw(canvas); } else if ((mEdge & EDGE_RIGHT) != 0) { - mShadowRight.setBounds(mRect.right, mRect.top, - mRect.right + mShadowRight.getIntrinsicWidth(), mRect.bottom); + mShadowRight.setBounds(RECT.right, RECT.top, + RECT.right + mShadowRight.getIntrinsicWidth(), RECT.bottom); mShadowRight.setAlpha(alpha); mShadowRight.draw(canvas); } else if ((mEdge & EDGE_TOP) != 0) { - mShadowTop.setBounds(mRect.left, mRect.top - mShadowTop.getIntrinsicHeight(), - mRect.right, mRect.top); + mShadowTop.setBounds(RECT.left, RECT.top - mShadowTop.getIntrinsicHeight(), + RECT.right, RECT.top); mShadowTop.setAlpha(alpha); mShadowTop.draw(canvas); } else if ((mEdge & EDGE_BOTTOM) != 0) { - mShadowBottom.setBounds(mRect.left, mRect.bottom, mRect.right, - mRect.bottom + mShadowBottom.getIntrinsicHeight()); + mShadowBottom.setBounds(RECT.left, RECT.bottom, RECT.right, + RECT.bottom + mShadowBottom.getIntrinsicHeight()); mShadowBottom.setAlpha(alpha); mShadowBottom.draw(canvas); } @@ -698,7 +698,7 @@ public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) { } @Override - public boolean isMoveActionValid() { + public boolean isValidMoveAction() { return mDrawComplete && mEnterAnimationComplete; } } diff --git a/slideback/src/main/java/com/phantomvk/slideback/SlideManager.java b/slideback/src/main/java/com/phantomvk/slideback/SlideManager.java index 6dc8a92..72fc4f7 100644 --- a/slideback/src/main/java/com/phantomvk/slideback/SlideManager.java +++ b/slideback/src/main/java/com/phantomvk/slideback/SlideManager.java @@ -15,6 +15,9 @@ import com.phantomvk.slideback.listener.SlideStateListener; public class SlideManager { + + private static final ColorDrawable DRAWABLE_TRANSPARENT = new ColorDrawable(Color.TRANSPARENT); + /** * The target activity to control. */ @@ -55,7 +58,7 @@ public SlideManager(@NonNull Activity activity, @Nullable SlideStateListener lis slideLayout = new SlideLayout(activity); slideLayout.addListener((listener == null) ? new SlideStateAdapter(activity) : listener); - activity.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); + activity.getWindow().setBackgroundDrawable(DRAWABLE_TRANSPARENT); } /** diff --git a/slideback/src/main/java/com/phantomvk/slideback/utility/ViewDragHelper.java b/slideback/src/main/java/com/phantomvk/slideback/utility/ViewDragHelper.java index b5de075..ec41d77 100644 --- a/slideback/src/main/java/com/phantomvk/slideback/utility/ViewDragHelper.java +++ b/slideback/src/main/java/com/phantomvk/slideback/utility/ViewDragHelper.java @@ -332,7 +332,7 @@ public int clampViewPositionVertical(@NonNull View child, int top, int dy) { * and enter animation are both completed in target activity. The extending * class must override this method and provide the flag. */ - public boolean isMoveActionValid() { + public boolean isValidMoveAction() { return false; } } @@ -1205,7 +1205,7 @@ public void processTouchEvent(@NonNull MotionEvent ev) { if (mDragState == STATE_DRAGGING) { // If pointer is invalid then skip the ACTION_MOVE. if (!isValidPointerForActionMove(mActivePointerId)) break; - if (!mCallback.isMoveActionValid()) break; + if (!mCallback.isValidMoveAction()) break; final int index = ev.findPointerIndex(mActivePointerId); final float x = ev.getX(index); diff --git a/slideback/src/main/res/values/attrs.xml b/slideback/src/main/res/values/attrs.xml index 16a145b..f69e5c0 100644 --- a/slideback/src/main/res/values/attrs.xml +++ b/slideback/src/main/res/values/attrs.xml @@ -15,4 +15,4 @@ - \ No newline at end of file + diff --git a/slideback/src/main/res/values/styles.xml b/slideback/src/main/res/values/styles.xml index 140cd89..ed49413 100644 --- a/slideback/src/main/res/values/styles.xml +++ b/slideback/src/main/res/values/styles.xml @@ -8,4 +8,4 @@ @drawable/drawable_slide_back_top @drawable/drawable_slide_back_bottom - \ No newline at end of file +