Skip to content

Commit

Permalink
Opt: Reuse constant object.
Browse files Browse the repository at this point in the history
  • Loading branch information
phantomVK committed Oct 17, 2019
1 parent facc6c8 commit abee8a1
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -697,7 +697,7 @@ public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) {
}

@Override
public boolean isMoveActionValid() {
public boolean isValidMoveAction() {
return mDrawComplete && mEnterAnimationComplete;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion slideback-support/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
</declare-styleable>

<attr name="SlideLayoutStyle" format="reference" />
</resources>
</resources>
2 changes: 1 addition & 1 deletion slideback-support/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
<item name="slide_back_shadow_top">@drawable/drawable_slide_back_top</item>
<item name="slide_back_shadow_bottom">@drawable/drawable_slide_back_bottom</item>
</style>
</resources>
</resources>
30 changes: 15 additions & 15 deletions slideback/src/main/java/com/phantomvk/slideback/SlideLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -698,7 +698,7 @@ public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) {
}

@Override
public boolean isMoveActionValid() {
public boolean isValidMoveAction() {
return mDrawComplete && mEnterAnimationComplete;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion slideback/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
</declare-styleable>

<attr name="SlideLayoutStyle" format="reference" />
</resources>
</resources>
2 changes: 1 addition & 1 deletion slideback/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
<item name="slide_back_shadow_top">@drawable/drawable_slide_back_top</item>
<item name="slide_back_shadow_bottom">@drawable/drawable_slide_back_bottom</item>
</style>
</resources>
</resources>

0 comments on commit abee8a1

Please sign in to comment.