Commit fc16325b authored by wajahat.s's avatar wajahat.s Committed by Commit bot

Creating RectF as member variable to save unnecessary allocation on each invocation of onDraw

When onDraw is called, everytime RectF is created which allocates new object. To compute the rect, it needs to be created only once and re-used every time when onDraw() is called altering its values, else too many RectF objects could affect performance.

This issue is found from below lint warning:
../../../tmp/tmppzTHqO/0/PopupZoomer.java:466 Avoid object allocations during draw/layout operations (preallocate and reuse instead): DrawAllocation [warning]
            RectF rect = new RectF();

Bug=None.

Review URL: https://codereview.chromium.org/549163002

Cr-Commit-Position: refs/heads/master@{#294349}
parent f357fb17
......@@ -122,6 +122,10 @@ class PopupZoomer extends View {
private GestureDetector mGestureDetector;
// These bounds are computed and valid for one execution of onDraw.
// Extracted to a member variable to save unnecessary allocations on each invocation.
private RectF mDrawRect;
private static float getOverlayCornerRadius(Context context) {
if (sOverlayCornerRadius == 0) {
try {
......@@ -401,6 +405,9 @@ class PopupZoomer extends View {
// Constrain initial scroll position within allowed bounds.
mPopupScrollX = constrain(mPopupScrollX, mMinScrollX, mMaxScrollX);
mPopupScrollY = constrain(mPopupScrollY, mMinScrollY, mMaxScrollY);
// Compute the bounds in onDraw()
mDrawRect = new RectF();
}
/*
......@@ -462,26 +469,25 @@ class PopupZoomer extends View {
float unshiftX = -mShiftX * (1.0f - fractionAnimation) / mScale;
float unshiftY = -mShiftY * (1.0f - fractionAnimation) / mScale;
// Compute the rect to show.
RectF rect = new RectF();
rect.left = mTouch.x - mLeftExtrusion * scale + unshiftX;
rect.top = mTouch.y - mTopExtrusion * scale + unshiftY;
rect.right = mTouch.x + mRightExtrusion * scale + unshiftX;
rect.bottom = mTouch.y + mBottomExtrusion * scale + unshiftY;
canvas.clipRect(rect);
// Compute the |mDrawRect| to show.
mDrawRect.left = mTouch.x - mLeftExtrusion * scale + unshiftX;
mDrawRect.top = mTouch.y - mTopExtrusion * scale + unshiftY;
mDrawRect.right = mTouch.x + mRightExtrusion * scale + unshiftX;
mDrawRect.bottom = mTouch.y + mBottomExtrusion * scale + unshiftY;
canvas.clipRect(mDrawRect);
// Since the canvas transform APIs all pre-concat the transformations, this is done in
// reverse order. The canvas is first scaled up, then shifted the appropriate amount of
// pixels.
canvas.scale(scale, scale, rect.left, rect.top);
canvas.scale(scale, scale, mDrawRect.left, mDrawRect.top);
canvas.translate(mPopupScrollX, mPopupScrollY);
canvas.drawBitmap(mZoomedBitmap, rect.left, rect.top, null);
canvas.drawBitmap(mZoomedBitmap, mDrawRect.left, mDrawRect.top, null);
canvas.restore();
Drawable overlayNineTile = getOverlayDrawable(getContext());
overlayNineTile.setBounds((int) rect.left - sOverlayPadding.left,
(int) rect.top - sOverlayPadding.top,
(int) rect.right + sOverlayPadding.right,
(int) rect.bottom + sOverlayPadding.bottom);
overlayNineTile.setBounds((int) mDrawRect.left - sOverlayPadding.left,
(int) mDrawRect.top - sOverlayPadding.top,
(int) mDrawRect.right + sOverlayPadding.right,
(int) mDrawRect.bottom + sOverlayPadding.bottom);
// TODO(nileshagrawal): We should use time here instead of fractionAnimation
// as fractionAnimaton is interpolated and can go over 1.
int alpha = constrain((int) (fractionAnimation * 255), 0, 255);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment